C++ 错误:应为非限定id
因此,我四处搜索答案,多人得到了相同的错误响应,但我看不出他们的错误如何修复我的代码 昨天我开始尝试C++,但我对大多数Python有经验,但C< 我正在制作一本简单的联系簿,只是为了学习。 我还没有完成这个类或任何其他实现,只是需要先修复这个问题 确切的错误是C++ 错误:应为非限定id,c++,C++,因此,我四处搜索答案,多人得到了相同的错误响应,但我看不出他们的错误如何修复我的代码 昨天我开始尝试C++,但我对大多数Python有经验,但C< 我正在制作一本简单的联系簿,只是为了学习。 我还没有完成这个类或任何其他实现,只是需要先修复这个问题 确切的错误是 kontaktbok.cpp:9: error: expected unqualified-id before 'public' 代码在这里 #include <iostream> #include <string&
kontaktbok.cpp:9: error: expected unqualified-id before 'public'
代码在这里
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool running = true;
public class Contact
{
private:
string firstname = "";
string lastname = "";
string phonenumber = "";
public:
Contact()
{
this.firstname = "Alfred";
}
};
int main()
{
cout << "Welcome to the Contact book!" << endl <<
"Made by X";
while (running)
{
cout << "1. Add contact" << endl
<< "2. List contacts" << endl
<< "3. Quit" << endl;
cout << "Choice: ";
string mystr;
getline(cin, mystr);
int choice;
stringstream(mystr) >> choice;
switch (choice)
{
case 1: cout << "asd";
case 2: cout << "asd2";
case 3: running = false;
default : cout << "Invalid integer";
}
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
bool running=true;
公共类联系人
{
私人:
字符串firstname=“”;
字符串lastname=“”;
字符串phonenumber=“”;
公众:
联系人()
{
this.firstname=“Alfred”;
}
};
int main()
{
CUT< P> <代码>公共类联系人< /代码>无效C++,使用<代码>类联系人< /代码>。 < P>这些更改将修复您的问题,公共类< /代码>无效c++,应该只是<代码>类< /代码>:
class Contact
{
private:
string firstname ;
string lastname ;
string phonenumber ;
public:
Contact()
{
this->firstname = "Alfred";
}
};
此外,当取消引用->
此时,您应该使用->
它是一个指针,而不是使用
将有效的普通结构或类。此外,成员变量应该在构造函数中初始化,如果您希望字符串为空,则默认构造函数将构造一个这都是我在描述中标记的内容,抱歉,如果我不清楚的话。不,你没有使用正确的标记(据我所知),我的意思是public class
类似于C#code。string firstname=“”在类定义中,C++中的?@ LuChanGrigor打算删除那些,谢谢<代码>字符串FiSTNEDIT=“”;<代码>是有效的C++ 11(虽然不必要)。