Unable to access const static members in class - c++

I have read this document
Initializing static array of strings (C++)? and tried to test in my compiler if everything would be fine here is copy of code
#include <iostream>
#include <string>
using namespace std;
class MyClass {
public:
const static char* MyClass::enumText[];
};
const char* MyClass::enumText={"a","b","c","d"};
int main(){
std::cout<<MyClass::enumText[0]<<endl;
return 0;
}
but here is mistakes
1>c:\users\david\documents\visual studio 2010\projects\class_static\class_static.cpp(9): error C2372: 'enumText' : redefinition; different types of indirection
1> c:\users\david\documents\visual studio 2010\projects\class_static\class_static.cpp(7) : see declaration of 'enumText'
1>c:\users\david\documents\visual studio 2010\projects\class_static\class_static.cpp(9): error C2078: too many initializers
i am using visual c++ 2010 and why such mistakes what is wrong?please help

That should be:
const char* MyClass::enumText[]={"a","b","c","d"};
// You forgot these ^^

You forgot the [] in the definition of the variable: const char* MyClass::enumText[]={"a","b","c","d"};

You missed []. It should be
const char* MyClass::enumText[]={"a","b","c","d"};

#include <iostream>
#include <string>
using namespace std;
class MyClass
{
public:
const static char* enumText[];
};
const char* MyClass::enumText[] = {"a","b","c","d"};
int main()
{
std::cout<<MyClass::enumText[0]<<endl;
return 0;
}

I think you're just missing the [] on the end of your definition of enumText (right before the ={...).

Related

Visual studio cannot determine the type of my global variables

What I have is :
#include "thread.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
vector<Requester*> requesters; //global
struct Requester {
vector<thread> t;
vector<int> tracks;
};
Then in my function I have:
void serviceQ(){
vector<Requester*> test = requesters; //error
}
The error is:
no suitable user-defined conversion from "std::vector<<error-type> *, std::allocator<<error-type> *>>" to "std::vector<Requester *, std::allocator<Requester *>>" exists
I'm very confused as to why this is. Why does it call my global variable an error type in the function? If I were to do something like:
void serviceQ(){
vector<Requester*> test;
//do some stuff
vector<Requester*> result = test; //no error
}
Then there is no error.
You need to define
vector<Requester*> requesters; //global
after the definition of struct Requester, as otherwise the compiler doesn't know what Requester* means when it attempts to define the corresponding vector<Requester*>. Alternatively, you can just declare
struct Requester;
above the line vector<Requester*> requesters;.

std::reference_wrapper on MS Visual Studio 2013

I try to compile some code which is very similar to:
#include <string>
#include <unordered_map>
class A{
};
int main(int argc, char* argv[]){
std::unordered_map<std::string, std::reference_wrapper<const A>> stringToRef;
A a;
const A& b = a;
stringToRef.insert(std::make_pair("Test", b));
return 0;
}
But can't figure out, why it's not compiling. I'm pretty sure, that the same code compiled fine on MS Visual Studio 2012 - but on Visual Studio 2013, it reports the following compilation error:
error C2280: std::reference_wrapper<const A>::reference_wrapper(_Ty &&): attempting to reference a deleted function
I tried to add copy, move, assignment operators to my class - but couldn't get rid of this error. How can I find out exactly, which deleted function this error refers to?
You want to store a std::reference_wrapper<const A>, so you can use [std::cref][1] to get that directly from a:
#include <functional>
#include <string>
#include <unordered_map>
#include <utility>
class A{
};
int main(int argc, char* argv []){
std::unordered_map<std::string, std::reference_wrapper<const A>> stringToRef;
A a;
stringToRef.insert(std::make_pair("Test", std::cref(a)));
return 0;
}
This works with GCC/Clang+libstdc++, Clang+libc++, and MSVS 2013 (tested locally).

Copy constructor error c2039

hy guys! I have a code that gives me headaches. I would like some help please. This is my .h file.
#include <iostream>
#include <string>
using namespace std;
namespace UI{
class Comanda
{
private:
const string _nume;
public:
Comanda();
Comanda(const string &nume);
virtual ~Comanda();
const string& Nume() const;
virtual void AsteaptaEnter();
virtual void Execute();
};
};
And the .cpp:
#include <iostream>
#include <string>
#include "Comanda.h"
#include "Exceptii.h"
using namespace std;
using namespace UI;
Comanda::Comanda()
{
cout << "Comanda()" << endl;
}
Comanda::Comanda(const string &nume)
{
_nume = nume._nume;
}
The compiler shows me this error:
error C2039: '_nume' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
What should i do? Thanks in advance!
You must initialize the constant members in the initializer list of ctor, and also nume._nume is not valid.
Comanda::Comanda(const string &nume) : _nume(nume) {}
^^^^^^^^^^^^^
You probably meant
Comanda::Comanda(const string &nume)
{
_nume = nume;
}
It's not a copy constructor by the way.
_nume = nume._nume; this is wrong
it should be _nume = nume;
And as correctly pointed out by #Cornstalks you cannot achieve above assignments in any case as _nume is const.

error C2039: 'Open' : is not a member of 'std::basic_fstream

When i call
void fileOpen(const char*
fname_){file_.Open(fname_,ios::in|ios::out|ios::ate|ios::binary);};
Function like
tempobj->fileOpen("LastID.dat");
It gives me the error
Error 23 error C2039: 'Open' : is not a member of 'std::basic_fstream<_Elem,_Traits>'
How do i resolve this. This is the class I have this function. It is template class
#ifndef FileHandlerh_h
#define FileHandlerh_h
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
template <class T>
class FileHandler
{
private:
fstream file_;
public:
FileHandler(){};
FileHandler(const char* fname_){fileOpen(fname_);};
void fileOpen(const char* fname_){file_.Open(fname_,ios::in|ios::out|ios::ate|ios::binary);};
void fileWrite(T);
void fileSeekWrite(T,int);
T fileRead(int);
int getNoOfRecords();
~FileHandler(){file_.close();};
};
Help me with this...!!
C++ is case sensitive. You need to use open() instead of Open().
Use a lowercase O, perhaps? It's quite uncommon to see capitals in function names in the standard library.

Global function header and implementation

how can I divide the header and implementation of a global function?
My way is:
split.h
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const string s, const string c);
split.cpp
#include "split.h"
void split(const string& s, const string& c){
...
}
main.cpp
// main.cpp : Defines the entry point for the console application.
//
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include "stdafx.h"
#include "split.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> v;
string s = "The;;woraaald;;is;;not;;enoaaaugh";
string c = " aaa ;; ccc";
split(s,c);
return 0;
}
And errors are:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...\split.h 8
Error 2 error C2146: syntax error : missing ',' before identifier 's' ...\split.h 8
How can I solve this problem? thx
In header file use std:: namespace qualifier - std::string
In the header file, you have to give the fully qualified name std::string. In the source files, you can add using namespace std; or using std::string; and then just spell it string.
Also, you've declared the function taking arguments by value, but defined it taking arguments by reference.
At least one problem is, you are missing the 'std::' namespace qualifier in split.h:
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const std::string s, const std::string c);
I think you either forgot to put using std::string; before split declaration or use std::string const& as split parameter declarations.
Also split declaration mismatch from split definition string const vs string const&