C++ -'Stream' Undeclared identifier - windows

Newbie here basically I want to load a file to the input stream. I get the following error
error C2065: 'Stream' : undeclared identifier.
#pragma once
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
namespace test2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
// ...
private: System::Void browse_Click(System::Object^ sender, System::EventArgs^ e) {
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
//code
myStream->Close();
}
}
}
};
}

The .NET Stream class is defined in the System.IO namespace, so you'll either need to…
qualify the name of the type in the object declaration and all subsequent uses
IO::Stream^ myStream;
or add a using directive to the top of your code file
using namespace System::IO;

You simply forgot to specify the correct namespace for the Stream class:
System::IO::Stream^ myStream;

Related

Cannot convert ‘boost::multiprecision::cpp_int

Giving this error on compilation:-
no matching function for call to ‘to_string(boost::multiprecision::cpp_int&)’ string s = to_string(i);
#include <boost/lexical_cast.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
#define int long long int
int32_t main()
{
mp::cpp_int l,i;
for(i=l;i<r;i++)
{
string s = to_string(i);
}
return 0;
}
You're including boost::lexical_cast's header file and boost::to_string. Include the proper header file for boost::to_string, which is "boost/exception/to_string.hpp", or use boost::lexical_cast.

Qt boost serialize compile errors on simple implementation

Platform specifics:
Fedora 21
g++ 4.9.2-6
Here's the error ....
$ g++ -c kabi-serial.cpp -lboost_serialization
kabi-serial.cpp: In function ‘void boost::serialization::kb_write_list()’:
kabi-serial.cpp:41:13: error: expected ‘;’ before ‘oa’
serialize oa(ofs);
kabi-serial.cpp:41:20: error: statement cannot resolve address of overloaded function
serialize oa(ofs);
^
kabi-serial.cpp:42:3: error: ‘oa’ was not declared in this scope
oa << ql;
And here's the kabi-serial.cpp source ...
#include <boost/serialization/vector.hpp>
#include <vector>
namespace boost {
namespace serialization {
class Cqnodelist
{
public:
friend class boost::serialization::access;
Cqnodelist(){}
std::vector<int>qnodelist;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & qnodelist;
}
};
void kb_write_list ()
{
Cqnodelist ql;
ofstream ofs("kabi-list.dat");
{
serialize oa(ofs);
oa << ql;
}
}
}
}
I based the source code on the simplest of the serialization examples, except using the serialization of the stl vector object.
I'm sure it's simple, but what am I missing?
To use ofstream you need to include <fstream>.
ofstream is in the namespace std: std::ofstream.
You don't need to put your code inside boost::serialization namespace (except case when you implement external to class serialize() function).
Adding serialize method implements way of loading and saving object attributes.
To actually store or load data you need to select storage — archive in Boost.Serialization terminology.
#include <boost/serialization/vector.hpp>
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
#include <fstream> // required for std::ofstream
class Cqnodelist
{
public:
friend class boost::serialization::access;
Cqnodelist(){}
std::vector<int>qnodelist;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & qnodelist;
}
};
void kb_write_list ()
{
Cqnodelist ql;
// fill object with data
ql.qnodelist.push_back(1);
ql.qnodelist.push_back(2);
ql.qnodelist.push_back(3);
// See http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/tutorial.html
// for reference
// save data to archive
{
// open file that will contain serialized data
std::ofstream ofs("kabi-list.dat");
// create archive on top of opened file
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << ql;
}
// to load object later open file, create input archive on top of opened
// file and load object state
{
Cqnodelist new_ql;
// create and open an archive for input
std::ifstream ifs("kabi-list.dat");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> new_ql;
}
}
int main()
{
kb_write_list();
}

How to write a C++ API class that will allow a user to register its own callback functions?

Let's say a user links his app against a library I wrote and I want to let him specify a callback function that I will call whenever an error occurs in my library. The implementation below works but I want to double check that I'm not missing something here:
Thread safety
DLL initialization issues
Public API considerations (I'm giving away a reference to an instance from the DLL is that OK?)
Anything that could be done better to hide implementation details from the public API?
errordispatcher.h:
#pragma once
#include <functional>
#include <memory>
#include <string>
namespace WE
{
class ErrorDispatcher
{
public:
ErrorDispatcher()
{}
explicit ErrorDispatcher(std::function<void(std::string)> user_func)
: error_callback_func{user_func}
{}
virtual ~ErrorDispatcher(){}
static ErrorDispatcher& getInstance()
{
return instance_;
}
void setErrorCallback(std::function<void(std::string)> user_func)
{
error_callback_func = nullptr;
if (user_func)
error_callback_func = user_func;
}
void dispatchError(std::string message)
{
if (error_callback_func)
error_callback_func(message);
}
private:
explicit ErrorDispatcher(const ErrorDispatcher&) = delete;
explicit ErrorDispatcher(ErrorDispatcher&&) = delete;
ErrorDispatcher& operator = (const ErrorDispatcher&) = delete;
ErrorDispatcher& operator = (ErrorDispatcher&&) = delete;
static ErrorDispatcher instance_;
std::function<void(std::string)> error_callback_func = nullptr;
};
}
NOTE: above I have inline implementation details in the public header to make this post shorter but they will be moved to a .cpp and won't be part of the public header
errordispatcher.cpp:
#include "errordispatcher.h"
namespace WE
{
ErrorDispatcher ErrorDispatcher::instance_;
}
apitest.h
namespace WE
{
void dllFunctionThatMightGiveError();
}
apitest.cpp
#include "errordispatcher.h"
#include "apitest.h"
namespace WE
{
void dllFunctionThatMightGiveError()
{
// Some error happens in dll so call user function and give a message to the user!
ErrorDispatcher::getInstance().dispatchError("Error in DLL!");
}
}
main.cpp (USER APP)
#include "errordispatcher.h"
#include "apitest.h"
#include <iostream>
#include <string>
void error_callback(std::string message)
{
std::cout << message << "\n";
}
int main(void)
{
WE::ErrorDispatcher::getInstance().setErrorCallback(error_callback);
WE::ErrorDispatcher::getInstance().dispatchError("Error in APP!");
WE::dllFunctionThatMightGiveError();
return 0;
}
Output is:
Error in APP!
Error in DLL!

Ambiguous class definition error in C++11 with std::hash

I tried to define int Hash(string key) in the hash.cpp file but it is giving me the error "hash is ambiguous".
I am not sure why. I have distributed the #includes in many ways and it still isn't working.
[File: hash.cpp]
#include "hash.h"
using namespace std;
int hash::Hash(string key)
{
}
[File: hash.h]
#include<string>
#include<cstdlib>
#include <iomanip>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash
{
public:
int Hash(string key);
};
#endif
[FILE: main.cpp]
#include<iostream>
#include "hash.h"
using namespace std;
int main()
{
return 0;
}
The problem is that C++11 already comes with std::hash (reference link) which results in the experienced conflict. Either remove the using namespace std; and put your class into an own namespace or rename the class to something different.

How to extend a listbox in VC++

I tried to extend a listbox by adding a couple functions. I get an error ( error C2144: syntax error : 'Extended_ListBox' should be preceded by ':'). Would anyone please teach me how to fix it? I went to the line which VC++ said there was the error, but I had no clue why the constructor had an error.
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::Collections::Generic;
#include <stdio.h>
#include <stdlib.h>
#using <mscorlib.dll>
public ref class Extended_ListBox: public ListBox{
public Extended_ListBox(array<String ^> ^ textLineArray, int counter){
textLineArray_store = gcnew array<String ^>(counter);
for (int i=0; i<counter; i++){
this->Items->Add(textLineArray[i]);
textLineArray_store[i] = textLineArray[i];
}
this->FormattingEnabled = true;
this->Size = System::Drawing::Size(380, 225);
this->TabIndex = 0;
this->SelectedIndexChanged += gcnew System::EventHandler(this, &Extended_ListBox::listBox1_SelectedIndexChanged);
}
public Extended_ListBox(){
this->FormattingEnabled = true;
this->Size = System::Drawing::Size(380, 225);
this->TabIndex = 0;
this->SelectedIndexChanged += gcnew System::EventHandler(this, &Extended_ListBox::listBox1_SelectedIndexChanged);
}
private: System::Void listBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
int index=this->SelectedIndex;
tempstring = textLineArray_store[index];
}
private: array<String ^> ^ textLineArray_store;
private: String ^tempstring;
public: String ^GetSelectedString(){
return tempstring;
}
public: void ListBox_Update(array <String ^> ^ textLineArray, int counter){
textLineArray_store = gcnew array<String ^>(counter);
for (int i=0; i<counter; i++){
this->Items->Add(textLineArray[i]);
textLineArray_store[i] = textLineArray[i];
}
}
};
In C++/CLI, you specify the access modifier (public, private, etc.) differently than in, say, C# or Java.
Instead, you just write one line (note the colon, which is required):
public:
and all the following members are public. So insert that line before your constructors and remove the public keyword before the constructors. Like that:
public ref class Extended_ListBox: public ListBox{
public:
Extended_ListBox(array<String ^> ^ textLineArray, int counter){
// constructor code
}
Extended_ListBox(){
// default constructor code
}
// other public members
// ...
private:
// private members
// ...
}
Similar to the members below the constructors in your current example, except that you don't have to explicitly restate public: or private: if the next member has the same visibility.

Resources