2016-03-04 136 views
0

当我编译这个时,我得到了我的名字函数在Name.cpp中的错误标题之前我从来没有见过这个错误。已经有一个在Name.cpp三大功能前一个构造函数,因为这是错误似乎什么是谈论。错误:预期的构造函数,析构函数或类型转换之前的'('令牌

的main.cpp

#include <iostream> 
#include <string> 
#include "Name.h" 
using namespace std; 

int main() 
{ 

} 

Name.h

#ifndef NAME_H 
#define NAME_H 
#include <iostream> 
#include <string> 
using namespace std; 

class Name 
{ 
    public: 
     Name(); 
     string getFirst(string newFirst); 

     string getMiddle(string newMiddle); 
     string getLast(string newLast); 
     void setFirst(); 
     void setLast(); 
     void setMiddle(); 

    private: 
    string First; 
    string Middle; 
    string Last; 
}; 

#endif // NAME_H 

Name.cpp

#include "Name.h" 
    #include <iostream> 
    #include <string> 
    using namespace std; 
    Name::Name() 
    { 

    } 

    Name::setFirst(newFirst){ 

     Name = newFirst; 
     cout << "You entered: " << Name << endl; 

    } 

    Name::setMiddle(newMiddle){ 

     Middle = newMiddle; 
     cout << "You entered: " << Middle << endl; 

    } 

    Name::setLast(newLast){ 

     Last = newLast; 
     cout<< "You entered: " << Last << endl; 

    } 
+4

题外话:除了你所面临的问题,那你可能需要一些常见问题解决得1.避免'在标题中使用名称空间标准,这很容易造成命名空间污染。 (所以在头文件中,改用'std :: string')。 2.考虑把你的输入参数改为'const std :: string&'3.设置你的输入参数。3.考虑把你的“getters”改为'const string&getBlablabla()const' –

+0

请你再解释一下为什么我应该避免命名空间标准? –

+1

不避免名称空间标准,你应该避免(尤其是在头文件中)使用名称空间(或其他'使用'指令)。在谷歌中搜索“C++ using namespace pollution”会给你提供很多信息 –

回答

3

您不能省略参数的类型名称。写一个。声明和定义中的函数原型也必须匹配。

Name.h应该

#ifndef NAME_H 
#define NAME_H 
#include <iostream> 
#include <string> 
using namespace std; 

class Name 
{ 
    public: 
     Name(); 
     string getFirst(); 

     string getMiddle(); 
     string getLast(); 
     void setFirst(string newFirst); 
     void setLast(string newLast); 
     void setMiddle(string newMiddle); 

    private: 
    string First; 
    string Middle; 
    string Last; 
}; 

#endif // NAME_H 

Name.cpp

#include "Name.h" 
#include <iostream> 
#include <string> 
using namespace std; 
Name::Name() 
{ 

} 

void Name::setFirst(string newFirst){ 

    Name = newFirst; 
    cout << "You entered: " << Name << endl; 

} 

void Name::setMiddle(string newMiddle){ 

    Middle = newMiddle; 
    cout << "You entered: " << Middle << endl; 

} 

void Name::setLast(string newLast){ 

    Last = newLast; 
    cout<< "You entered: " << Last << endl; 

} 
相关问题