-1
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|

这不是从类中调用变量的方法吗?类名.变量/函数名?

编辑:所以在创建我的 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'|
4

1 回答 1

3

你需要一个类的实例Vars

Vars v;
ofstream outfile(v.dir + "\\data");
于 2013-05-20T13:54:13.423 回答