c ++成员函数错误:在'。'之前预期','或'...'代币

时间:2016-12-17 06:44:01

标签: c++ string class constructor linker

所以我在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不兼容或没有更新。如果我不使用cstr,我会收到另一个错误。)

1 个答案:

答案 0 :(得分:2)

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

所以你的构造函数应该是InputData(string filea, string fileb)

要编译为C++11,请使用gcc编译器标志-std=c++11