开发者

expected class-name before '{' token with no headers

开发者 https://www.devze.com 2023-03-22 15:30 出处:网络
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.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消