所以,我四处寻找答案,多人得到相同的错误响应,但我看不出他们的错误如何修复我的代码。
我昨天开始尝试 C++,但我对 Python 和 C# 都很有经验
我正在制作一个简单的通讯录,只是为了学习。我没有完成类或任何其他实现,只需要先修复它。
确切的错误是
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;
}
提前致谢!