Robin Douglas
Robin Douglas

Reputation: 43

Error no member function declared in class when compiling

I am pretty new to c++ and have no idea why I am getting this error, except that I think it's to do with using the string type for getter methods.

The error message:

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

Upvotes: 1

Views: 1215

Answers (1)

Amnon Shochot
Amnon Shochot

Reputation: 9386

The following code defines fields (variables) rather then methods.

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

Then, when you implement it in the .cpp file the compiler complains that you try to implement a method that was not declared (since you declared get_name to be a variable).

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

To fix, just change your code as below:

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

Upvotes: 2

Related Questions