C++ 成员函数错误:在'.'令牌之前需要','或'...'

c++ member function error: expected a ',' or '...' before '.' token

本文关键字:令牌 函数 C++ 成员 错误      更新时间:2023-10-16

所以我在Visual Studio 2015中写了一个程序,并尝试将其转移到Ubuntu。除了问题,我还没有遇到任何问题。

我有一个名为" InputData"的类,带有一个构造函数,该类别需要两个字符串将其放入ifstream中:

class InputData {
( ... )
public:
    InputData(string filea.c_str(), string fileb.c_str());
}

,在我的构造函数中,我有

InputData::InputData(string filea.c_str(), string fileb.c_str()) 
{
    ifstream instream;
    instream.open(filea.c_str());
    ( ... )

最后,在我的主要功能中,我有

InputData x ("firstfile.csv", "secondfile.csv");

但是,当我尝试将它们链接在一起并编译时,我会收到错误:

expected a ',' or '...' before '.' token
  InputData(string filea.c_str(), string fileb.c_str());
                       ^

我以前从未见过这种错误,我不知道有什么问题。谁能帮忙?

(另外,我正在使用.cstr(),因为我的编译器由于某种原因与C 11不兼容或不更新到C 11。如果我不使用CSTR,我会遇到另一个错误。>

编译器错误是因为在构造函数声明中,每个参数必须具有指定类型和该类型的变量 name ,就像任何C 函数一样。但是变量名不能包括'.'字符,因此错误:filea.c_str()不是有效的变量名称。这是一个函数调用。

,您的构造函数应该像InputData(string filea, string fileb)

编译为C++11使用GCC编译器标志-std=c++11

相关文章: