2016-05-31 106 views
0

编译器说:学生不是类或名称空间的名称。它说“名称”是未申报的。问题与类或名称空间的名称

students.cpp:

#include "stdafx.h" 
#include <string> 
#include "students.h" 
using namespace std; 
    void students::set_name(string student_name) 
    { 
     name = student_name; 
    } 

students.h:

#pragma once 
#include <string> 
#include "students.cpp" 
using namespace std; 
class students 
{ public: 
      void set_name(string student_name); 
    private: 
      string name; 
}; 

的main.cpp

#include "stdafx.h" 
#include <iostream> 
#include <cstdlib> 
#include <string> 
#include "students.h" 
#include "students.cpp" 
using namespace std; 
    int main() 
    { 
     students *student = new students; 
     std::string name; 
     std::cout << "Name: "; 
     getline(std::cin, name); 
     student->set_name(name); 
     delete student; 
     system("pause"); 
     return 0; 
    } 

更新:我已经删除后 “的#include” students.cpp “”我得到了:

1>students.obj : error LNK2005: "public: void __thiscall students::set_name(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) уже определен в main.obj 

回答

3

你不应该包括在students.h文件.cpp文件,只是省略

#include "students.cpp" 

额外的小费:省略

using namespace std; 
在头文件

,以避免发生命名空间冲突,看到这个question here

0

LNK2005意味着符号已经定义,你说:

#include "students.cpp" 

在您的源文件的两个地方,因此您有多个定义为您的students::set_name(string student_name)。您不应该在源文件中包含.cpp文件。在您的项目中添加students.cpp,然后删除源中的所有发生的#include "students.cpp"

0

您应该从main.cpp和students.h中删除#include "students.cpp"

相关问题