2015-04-05 112 views
1

我对C++很新颖,不知道为什么我得到这个错误,除了我认为它是为getter方法使用字符串类型。错误在编译时没有在类中声明的成员函数

错误消息:

C:\Users\Robin Douglas\Desktop\week6>g++ -c Student.cpp 
Student.cpp:15:31: error: no 'std::string Student::get_name()' member function d 
eclared in class 'Student' 
Student.cpp:20:43: error: no 'std::string Student::get_degree_programme()' membe 
r function declared in class 'Student' 
Student.cpp:25:32: error: no 'std::string Student::get_level()' member function 
declared in class 'Student' 

Student.hpp

#include <string> 

class Student 
{ 
    public: 
     Student(std::string, std::string, std::string); 
     std::string get_name; 
     std::string get_degree_programme; 
     std::string get_level; 
    private: 
     std::string name; 
     std::string degree_programme; 
     std::string level; 
}; 

Student.cpp

#include <string> 
#include "Student.hpp" 

Student::Student(std::string n, std::string d, std::string l) 
{ 
    name = n; 
    degree_programme = d; 
    level = l; 
} 

std::string Student::get_name() 
{ 
    return name; 
} 

std::string Student::get_degree_programme() 
{ 
    return degree_programme; 
} 

std::string Student::get_level() 
{ 
    return level; 
} 

回答

2

下面的代码定义字段(变量),而随后的方法。

public: 
    Student(std::string, std::string, std::string); 
    std::string get_name; 
    std::string get_degree_programme; 
    std::string get_level; 

然后,当你实现它的.cpp文件编译器会抱怨你试图实现未声明的方法(因为你宣布GET_NAME是一个变量)。

std::string Student::get_name() 
{ 
    return name; 
} 

要解决,只是改变你的代码如下:

public: 
    Student(std::string, std::string, std::string); 
    std::string get_name(); 
    std::string get_degree_programme(); 
    std::string get_level(); 
+0

谢谢,这个工作:) – 2015-04-05 21:09:39

相关问题