Class has no member named

前端 未结 9 1169
甜味超标
甜味超标 2021-02-19 01:00

I have a problem with accessing a function from a class with the class object in my main function. I am just trying to make the object for the class and use that ob

相关标签:
9条回答
  • 2021-02-19 01:31

    Try to define the functions right into the header

     #ifndef ATTACK_H
     #define ATTACK_H
    
     class Attack {
         public:
             Attack(){};
             void printShiz(){};
         protected:
         private: };
    
     #endif // ATTACK_H
    

    and to compile. If the compiler doesn't complain about duplicate definitions it means you forgot to compile the Class.cpp file, then you simply need to do it (add it to your Makefile/project/solution... which toolchain are you using?)

    0 讨论(0)
  • 2021-02-19 01:32

    Do you have a typo in your .h? I once came across this error when i had the method properly called in my main, but with a typo in the .h/.cpp (a "g" vs a "q" in the method name, which made it kinda difficult to spot). It falls under the "copy/paste error" category.

    0 讨论(0)
  • 2021-02-19 01:32

    I know this is a year old but I just came across it with the same problem. My problem was that I didn't have a constructor in my implementation file. I think the problem here could be the comment marks at the end of the header file after the #endif...

    0 讨论(0)
  • 2021-02-19 01:42

    I had similar problem. My header file which included the definition of the class wasn't working. I wasn't able to use the member functions of that class. So i simply copied my class to another header file. Now its working all ok.

    0 讨论(0)
  • 2021-02-19 01:50

    Most of the time, the problem is due to some error on the human side. In my case, I was using some classes whose names are similar. I have added the empty() method under one class; however, my code was calling the empty() method from another class. At that moment, the mind was stuck. I was running make clean, and remake thinking that it was some older version of the header got used. After walking away for a moment, I found that problem right away. We programmers tends to blame others first. Maybe we should insist on ourselves to be wrong first.

    Sometimes, I forget to write the latest update to disk and looking at the correct version of the code, but the compiler is seeing the wrong version of the code. This situation may be less a issue on IDE (I use vi to do coding).

    0 讨论(0)
  • 2021-02-19 01:53

    Did you remember to include the closing brace in main?

    #include <iostream>
    #include "Attack.h"
    
    using namespace std;
    
    int main()
    {
      Attack attackObj;
      attackObj.printShiz();
    }
    
    0 讨论(0)
提交回复
热议问题