5

I regard myself as a fairly novice c++ programmer and I have never experienced this error before.

I'm simply trying to create a class for my function but all my std:: prefixed functions declared in my header file are not being recognised

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person
{
    public:
        Person();
        std::string getName();  //return first name
        std::string getSurname();//return surname
        int getWeight();    //return weight
        int getBirthYear(); //return birthyear


    private:
//self explanatory member variables but need to be accessible to patient
        std::string m_name;
        std::string m_surname;
        int m_weight;
        int m_birthYear;
};

#endif      

.cpp

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#include "Person.h"

Person::Person()
{
    m_name = "name";
    m_surname = "surname";
    m_weight = 0;
    m_birthYear = 0;
    return;
}

//returns m_name
std::string Person::getName()   
{
    return m_name;
}

//returns m_surname
std::string Person::getSurname()
{
    return m_surname;
}

//returns persnon's weight
int Person::getWeight()
{
    return m_weight;
}   

//returns the person's birth year
int Person::getBirthYear()
{
    return m_birthYear;
}

main

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#include "Person.h"
#include <iostream>

using namespace std;

int main()
{
//  Person matt;
//  cout << matt.getName() << endl;
//  cout << matt.getSurname() << endl;
//  cout << matt.getWeight() << endl;
//  cout << matt.getBirthYear() << endl;
    return 0;
}

And this is the error I'm receiving

g++ Main.cpp Person.h Person.cpp -o test
Person.cpp: In constructor ‘Person::Person()’:
Person.cpp:17:2: error: ‘m_name’ was not declared in this scope
Person.cpp:18:2: error: ‘m_surname’ was not declared in this scope
Person.cpp: At global scope:
Person.cpp:35:29: error: no ‘std::string Person::getName()’ member function declared in class ‘Person’
Person.cpp:41:32: error: no ‘std::string Person::getSurname()’ member function declared in class ‘Person’

Any ideas what I'm doing wrong? This exact same std:: formatting has worked for me before but for some reason now only the std::string functions aren't recognised when trying to create a simple Person class.

16
  • 7
    What's the header doing in your build command? And what do people have against constructor initializer lists? Commented Apr 28, 2013 at 6:34
  • Unfortunately this is the only way I've been taught to program and its preferred by my tutors/markers as its easier for them to skim Commented Apr 28, 2013 at 6:36
  • 8
    Well your tutors are wrong about this. Ask for your money back. Commented Apr 28, 2013 at 6:37
  • 1
    Please provide the actual code that is failing to compile. The first error message refers to line 17, but it is not line 17 in what you included in your question. Commented Apr 28, 2013 at 6:38
  • 3
    Do you by any chance have any .pch files in your project directory? Including the header in the build command is only done for precompiled headers, so you might just have a precompiled older version of Person.h. Commented Apr 28, 2013 at 6:54

1 Answer 1

9

From the comments:

g++ Main.cpp Person.h Person.cpp -o test

As chris pointed out, it is unusual to include the header files in the compile command line. A slightly different invocation that you probably used earlier:

g++ -c Main.cpp Person.h Person.cpp

creates Main.o, Person.o, but also a Person.h.gch precompiled header. The precompiled header isn't regenerated with your current build command, but is still used, so changes to Person.h do not get picked up.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.