class Vars{
public:
char *appData = getenv("AppData");
string dir = strcat(appData, "\\Adam");
};
这是我的班级
这就是我试图使用它的方式:
void write(string data){
ofstream outfile(Vars.dir + "\\data"); //here's the error
outfile << data;
outfile.close();
}
这是错误:
main.cpp|88|error: expected ',' or '...' before '.' token|
这不是从类中调用变量的方法吗? Classname.variable / functionname?
编辑:所以在制作Vars类的实例后,我遇到了这些错误:main.cpp|19|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
main.cpp|20|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
main.cpp||In function 'void write(std::string)':|
main.cpp|88|error: no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'|
main.cpp|88|note: candidate is:|
mingw32\4.7.1\include\c++\fstream|702|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|702|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'|
main.cpp||In function 'std::string read(std::string)':|
main.cpp|96|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'|
main.cpp|96|note: candidate is:|
mingw32\4.7.1\include\c++\fstream|531|note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
mingw32\4.7.1\include\c++\fstream|531|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'|
main.cpp||In function 'int read(int)':|
main.cpp|107|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'|
main.cpp|107|note: candidate is:|
mingw32\4.7.1\include\c++\fstream|531|note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
mingw32\4.7.1\include\c++\fstream|531|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'|
main.cpp|108|error: expected ';' before 'if'|
答案 0 :(得分:3)
您需要Vars
类的实例:
Vars v;
ofstream outfile(v.dir + "\\data");