22

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 object to access the function inside that class's .cpp file. I keep getting an error and I even made the simplest program to test it and I still get an error.

Main:

#include <iostream>
#include "Attack.h"

using namespace std;

int main()
{
    Attack attackObj;
    attackObj.printShiz();
}

Class header:

#ifndef ATTACK_H
#define ATTACK_H

class Attack
{
    public:
        Attack();
        void printShiz();
    protected:
    private:
};

#endif // ATTACK_H

Class .cpp:

#include <iostream>
#include "Attack.h"
using namespace std;

Attack::Attack() {

}

void Attack::printShiz() {
    cout << "Test" << endl;
}

How do I fix this error? Everytime I try to access the printShiz() function in the Attack class by using an object in my main function, I get an error and it doesn't think this function exists within this class.

Error:

error: 'class Attack' has no member named 'printShiz'

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Rapture686
  • 411
  • 1
  • 4
  • 9
  • 6
    The code looks fine. Maybe it's trying to use an older version of the header. – chris Jul 05 '13 at 06:05
  • 7
    Sometimes a "Rebuild All" fix everything. – johnchen902 Jul 05 '13 at 06:07
  • Just tried it, getting the same error :/ – Rapture686 Jul 05 '13 at 06:09
  • You should post some code that reproduces the problem. The code you posted looks fine. – juanchopanza Jul 05 '13 at 06:12
  • suggest 2 : copy printShiz() and replace all with copied. sometimes what it is written seems equal but when you change to ANsi on notepad++ you see that under code is different. happen when you switch much between keyboard layouts – qwr Jul 05 '13 at 06:12
  • The problem must be with your include path, directories/filenames etc.. Which compiler are you using? If e.g. GCC, then you can use "g++ -E main.cc " to get proprocessor-stage output that will show if the header you expect is being incorporated properly in the translation unit. – Tony Delroy Jul 05 '13 at 06:14
  • I'm using Codeblocks with the standard GNU GCC Compiler it came with. I just started coding so I don't know too much about what is wrong. – Rapture686 Jul 05 '13 at 06:22
  • would you please give me exact command you are using to compile your code? I'm afraid that you are not linking class.o object with your code properly. – Boynux Jul 05 '13 at 06:30
  • @Boynux I'm really new to the whole coding thing so I dont know exactly what you mean by the command. I know that I'm using Codeblocks with the GNU GCC Compiler. When I go to the compiler settings under toolchain executables, it says the C++ compiler is mingw32-g++.exe – Rapture686 Jul 05 '13 at 06:38
  • open a terminal and witch to your program folder and then issue this command: `mingw32-g++.exe main.cpp class.cpp -o main.exe` then try to run `main.exe` – Boynux Jul 05 '13 at 07:04
  • How do I do that? As I said I'm VERY new to coding. I just started a couple days ago. I just want to fix whatever is wrong so I can go back to learning how to code. – Rapture686 Jul 05 '13 at 07:07
  • what windows version do you use? – Boynux Jul 05 '13 at 07:12
  • I am currently using Windows 7 – Rapture686 Jul 05 '13 at 07:15
  • well goto start -> run -> type cmd.exe and enter then follow my previous comment. – Boynux Jul 05 '13 at 07:21
  • I navigated to the program's folder and entered the command exactly and got a response: "'ming32-g++.exe' is not recognized as an internal or external command, operable program or batch file." – Rapture686 Jul 05 '13 at 07:30
  • so find `mingw32-g++.exe` and use its absolute path to call (like `c:\some\folder\mingw32-g++.exe main.cpp class.cpp -o main.exe`) – SpongeBobFan Jul 05 '13 at 07:39
  • Since you say you are new to programming let me give you a suggestion, always include your own headers first (see your main.cpp) so that you make sure that you are including everything you need in other files. Your example is just fine, this is just a suggestion! – Stefano Falasca Jul 05 '13 at 08:28
  • Thanks for the advice! My current problem is currently keeping me from programming and I have no idea why it's happening so I'm kinda bummed out. Hopefully this problem will be fixed soon – Rapture686 Jul 05 '13 at 08:33
  • you may also consider testing your code here: http://www.compileonline.com/compile_cpp_online.php – Boynux Jul 05 '13 at 09:18
  • Please move extended discussions to [chat]. Thank you. – Tim Post Jul 05 '13 at 12:17

11 Answers11

23

I had a similar problem. It turned out, I was including an old header file of the same name from an old folder. I deleted the old file changed the #include directive to point to my new file and all was good.

asde
  • 231
  • 2
  • 3
  • This is the correct answer to the question. It happens sometimes. You copy a header file to another path and make changes to it, but because somewhere in your project you include the old header, changes to member functions, variables etc never appear and you get this kind of "strange" errors. – Yiannis Mpourkelis Jan 18 '17 at 18:24
  • Having a compile error rightaway is almost the best result you can get from including the wrong header. In the worse case the code compiles but may crash in the destructor (or any other strange places) due to undefined behavior caused by ODR violation. I have once spent very long time debugging an issue like that. – Weijun Zhou Jul 13 '23 at 03:52
2

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).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
1

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.

1

I had a similar problem in VSCode after copy/pasting *.h and *.cpp files from a base folder into a subfolder. The issue was that one file in the base folder was referencing one *.h file, that has been moved into the subfolder. I could resolve the issue by explicitly writing the relative path w.r.t. the base folder in each include statement, e.g.:

  • for a file in the SUBfolder that includes base_header.h from the BASE folder: #include "../base_header.h"
  • for a file in the BASE folder that includes sub_header.h from the SUBfolder: #include "subfolder/sub_header.h"

I did this for each header, and had no issues afterwards.

Another issue was strange behaviour due to a duplicate import, so #pragma once in the header file might also help.

asti205
  • 154
  • 7
  • 1
    Gosh... 1.5 hours later you've solved the mystery. I had MyClass defined in a file I copied somewhere else and never got rid of. The weird thing was it would work in parts of my code and other parts not, I guess it was just based on whether we included the old file first or not. I was blocking it with #pragma once so I didn't even get the redefinition errors. – user267298 Apr 11 '23 at 23:29
0

Did you remember to include the closing brace in main?

#include <iostream>
#include "Attack.h"

using namespace std;

int main()
{
  Attack attackObj;
  attackObj.printShiz();
}
Norwæ
  • 1,575
  • 8
  • 18
0

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?)

Stefano Falasca
  • 8,837
  • 2
  • 18
  • 24
0

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...

normyp
  • 184
  • 1
  • 11
0

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.

vesperto
  • 804
  • 1
  • 6
  • 26
0

Maybe I am 6.5 years late. But I'm answering because others maybe searching still now. I faced the same problem and was searching everywhere. But then I realized I had written my code in an empty file. If you create a project and write your code in there, there won't be this error.

0

The reason that the error is occuring is because all the files are not being recognized as being in the same project directory. The easiest way to fix this is to simply create a new project.

File -> Project -> Console application -> Next -> select C or C++ -> Name the project and select the folder to create the project in -> then click finish.

Then to create the class and header files by clicking New -> Class. Give the class a name and uncheck "Use relative path." Make sure you are creating the class and header file in the same project folder.

After these steps, the left side of the IDE will display the Sources and Headers folders, with main.cpp, theclassname.cpp, and theclassname.h all conviently arranged.

minTwin
  • 1,181
  • 2
  • 21
  • 35
0

My Solution has two separate projects, Assignment4a and Assignment4b, both making use of a 'Temp.h' file. I copy-pasted the entire file from project a to project b, and even though I added much more to the second 'Temp.h' file it gave me the same error for each and every new item (i.e. class has no member named ...).

The second project, Assignment4b, was attempting to use the 'Temp.h' file located in the first project, Assignment4a, regardless of inserting the #pragma once or any of the other suggested fixes above.

The one that did work however was explicitly defining the path to the 'Temp.h' file in the second project, Assignment4b.

#include "../Assignment4b/Temp.h"

Did not have to change anything in the first project, just the addition of the relative path for project Assignment4b was enough, both projects work flawlessly now.

starball
  • 20,030
  • 7
  • 43
  • 238