0

Compiler say:students isn't the name of the class or of the namespace. And it say that "name" is undeclared .

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

update: after i've removed " #include "students.cpp" " i got:

 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> >)" (?set_name@students@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) уже определен в main.obj
Timur
  • 23
  • 6

3 Answers3

3

You should not include the .cpp file in the students.h file, just omit

#include "students.cpp"

Additional tip: Omit

using namespace std;

in the header file to avoid namespace clashes, see this question here

Community
  • 1
  • 1
CppChris
  • 1,226
  • 9
  • 14
0

LNK2005 means that symbol was already defined, you have added:

#include "students.cpp"

in two places in your source files so you have multiple definitions for your students::set_name(string student_name). You should never include .cpp files in source files. Add students.cpp in your project, then remove all occurences of #include "students.cpp" in your sources.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

You should remove #include "students.cpp" from both main.cpp and students.h.

Maria Ivanova
  • 1,146
  • 10
  • 19