0

I'm rather confused on the answers I find on the error at hand.

I have one class which i want to inherit from. But when I try to inherit I get the said error. Non of these classes use header files, since i'm only doing this to learn how virtual overrides work. I'm pretty new to C++. So this isn't about actually making something, more like understanding C++ better.

When I look for the answer on google, It usually ends up in problems with header files and "#include" keywords.

And there isn't much code to work with either. Any suggestions?

edit:

Like i said not much code to work with, also the other files I didnt include are no different than Pianist

   /* 
 * File:   main.cpp
 * Author: Sidar
 *
 * Created on 19 juli 2011, 17:51
 */

#include <cstdlib>
#include <iostream>
#include "Musician.cpp"
#include "Pianist.cpp"
#include "MasterPianist.cpp"
#include "JuniorPianist.cpp"


using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    Musician *m = new Musician();
    Pianist *p = new Pianist();
    //JuniorPianist *jp = new JuniorPianist();
    MasterPianist *mp  = new MasterPianist();
    //__________________________
    cout << "Pianist greets:\n";
    m->greet();
    cout << "And this is a:\n";
    m = p;
    m->greet();
    cout << "The pianist states his proffesion:\n";
    m = mp;
    m->greet();
    cout << "And this is his student:\n";
   // m = jp;
    m->greet();

    return 0;
}

Class to be inherited

    #include <iostream>
using namespace std;

class Musician{

public:
    //Constructors
    Musician(){}
    Musician(const Musician& m){}
    ~Musician(){}
    //Methods/Functions
    virtual void greet(){
        cout << "Hello";
    }
};

Class which tries to inherit.

 /* 
 * File:   Pianonist.cpp
 * Author: Sidar
 * 
 * Created on 19 juli 2011, 17:58
 */

#include <iostream>
using namespace std;

class Pianist: public Musician {

public:
    Pianist(){}
    Pianist(const Pianist& orig){}
    ~Pianist() {}
    //____________________________________
    void greet(){
        cout << " This is  pianist";
    }
};
Sidar
  • 1
  • 1

1 Answers1

0

It seems like your class Pianist inherits from Muscician in the file Pianist.cpp (which should be renamed Pianist.hpp) but that file does not include Musician.cpp (which should be renamed Musician.hpp ) and so does not know what Muscician is. You want to include the definition of Musicain into Pianist.hpp

Now, when you do this, you will probably find that the compiler has passed each of those files more than once, breaking the one-definition-rule. To overcome this, you need to add include-guards to each of your included files. Try adding #pragma once as the first line to each of the header files. (note #pragma once is not cross platform, but most implementations support it, so you should be okay until you are a bit more familiar with the language).

In short,

  1. Rename Musician.cpp -> Musician.hpp (likewise for Pianist). These are header files (you include them in your main.cpp)
  2. add #include "Musician.hpp" to Pianist.hpp (so the Musician class can find the definition (and implementation) of the Musician class.
  3. Add #pragma once to the top of all your .hpp files.

then give any compiler error that you have after you have done this.

Tom
  • 5,219
  • 2
  • 29
  • 45
  • 2
    Just curious: why make [include guards](http://en.wikipedia.org/wiki/Include_guard) sound so mysterious? :) It's a pretty simple concept and not something I would avoid until more familiar with the language. – Bart Jul 25 '11 at 19:12
  • @Bart, fair enough - I didn't mean to make them sound mysterious. However, they require quite a lot of typing and can cause strange compiler messages if you forget to unlock the guard at the end of the file (or use the same guard name in two different files). Indeed, this question could be about the compiler not being able to find a piece of code, a problem that can be caused by guards used wrongly. On the other hand, it is certain that `#pragma once` will not contribute to the problems Sidar is having. – Tom Jul 25 '11 at 19:18
  • I hardly follow what you are saying. Isn't hpp indicating a header file? I'm not using headers... – Sidar Jul 25 '11 at 19:19
  • @Sidar, you are using headers - you include headers in other files. You have just called them (incorrectly) cpp files. – Tom Jul 25 '11 at 19:22
  • @Tom There are no hpp files! only cpp files. Nothing is incorrect. I have no header files. – Sidar Jul 25 '11 at 19:23
  • @Sidar - There should be `.hpp` files. Unless all your code is in main.cpp, then you need `.hpp` files. You should not include implementation files in `main.cpp`, and you have done that. I have updated my answer to make it more clear. – Tom Jul 25 '11 at 19:29
  • It is not clear to me at all why I should change the extention file to hpp. I can not blindly follow instructions without any explanations to why i should change it. The content of these files look nothing like headers at all. – Sidar Jul 25 '11 at 19:29
  • 3
    We're saying that [you should have](http://stackoverflow.com/questions/333889/in-c-why-have-header-files-and-cpp-files), not that you do have. That is, you should separate your code into a header (.hpp or .h) and implementation (.cpp) file. Actually Tom is giving you the answer that I was hinting at. You can then subsequently include the appropriate header file (Musician.h) in Pianist.cpp for example, so it knows what this Musician is you're referring to. – Bart Jul 25 '11 at 19:31
  • @Sidar, you are allowed to put the implementation within you header files. However, the class definitions (with or without the implementation) should be put in a header file, which you then include in other files (such as main.cpp) – Tom Jul 25 '11 at 19:31
  • I've been told that classes will work without headers, only that when they are compiled its impossible to reference to their methods and members. ( when working with DLLs ) Ill try to sort this out with headers, i just didn't think they were needed. – Sidar Jul 25 '11 at 19:33