Undeclared Identifier - Inheritance and Header Files

I've setup my code as seen below. You can also find the errors that I am receiving below the code.

B.cpp
1
2
3
4
5
6
7
8
9
10
11
  #include "B.h"

B::B() 
    : f(), i(), b(), s() // **
 { }

void B::setValues() { }

std::string printValues() {
    return s + " " + std::to_string(i) + " " + std::to_string(f) + " " + std::to_string(b);
}


B.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef B_H
#define B_H
#include <string> // to be used here, so we need to include it
// not "using namespace std;" here!! *
class B 
{
public:
	std::string s;
    float f;
    int i;
    bool b;

    B();
    void setValues();
    std::string printValues(); // don't miss that std::
};

#endif 


S.cpp
1
2
3
4
5
6
7
8
#include "S.h"

S::S(std::string name) : s2(name) // **
{ }

std::string subPrint () { // ***
    return printValues() + s2;
}


S.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef S_H
#define S_H
#include "B.h" // required to make B known here
#include <string> // known through B, but better safe than sorry
class S : public B 
{
public:
    S(std::string name);
    std::string subPrint(); // ***
protected:
	std::string s2;
};
#endif 


Not sure how to insert an image, but here is the link to an imgur of my errors: http://i.imgur.com/FSlXLKs.png
You forgot to qualify some of your implementations with the name of the class - e.g. B::printValues and S::subPrint - thus the compiler sees them as global functions instead of member functions.
Last edited on
Which ones specifically? I've tried that and it didnt give me the intended result. Thanks for the response!
Which ones specifically? I've tried that and it didnt give me the intended result.


The ones that LB mentioned. You need to do it in the cpp files. Post your new code if there are still issues.

Good Luck :+)
Topic archived. No new replies allowed.