2013-03-15 162 views
2

所以,我已经搜索周围的答案,并且多人得到了相同的错误响应,但我看不出他们的错误如何修复我的代码。error:expected unqualified-id

我开始尝试C++昨天,但我经历了大多的Python也是C#

我想提出一个简单的通讯录,只是为了学习。 我没有完成类或任何其他实现,只需要这个固定的第一。

确切的错误是

​​

而且代码是在这里

#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; 
} 

提前感谢!

+0

这是C++,而不是C#。 – 2013-03-15 16:41:21

+0

这就是我把它标记为和描述中写的,对不起,如果我不够清楚 – 2013-03-15 16:42:31

+1

不,你没有错误地使用正确的标签(据我所知),我的意思是说,公共类'类似于C#代码。 – 2013-03-15 16:45:27

回答

5

这些变化将解决您的问题,public Class是无效的C++,它应该只是class其中使用.将是有效的。此外,成员变量应该在构造函数中初始化,如果你想要空string s默认构造函数构造一个empty string

+0

'string firstname =“”;'在C++中的类定义中? – 2013-03-15 16:43:40

+0

@LuchianGrigore打算删除那些,谢谢 – 2013-03-15 16:44:27

+0

谢谢!这做到了! – 2013-03-15 16:49:02

6

public class Contact C++无效,请使用class Contact代替。提领this

class Contact 
{ 
    private: 
     string firstname ; 
     string lastname ; 
     string phonenumber ; 

    public: 
    Contact() 
    { 
     this->firstname = "Alfred"; 
    } 

    }; 

此外,你应该使用->它是一个指针不是普通structclass

相关问题