Class has no member named ошибка

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's user avatar

0xCursor

2,2424 gold badges15 silver badges33 bronze badges

asked Jul 5, 2013 at 6:02

Rapture686's user avatar

20

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.

answered Sep 23, 2014 at 19:50

asde's user avatar

asdeasde

2312 silver badges3 bronze badges

1

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's user avatar

Adrian Mole

49.5k155 gold badges49 silver badges79 bronze badges

answered Aug 23, 2018 at 22:01

Kemin Zhou's user avatar

Kemin ZhouKemin Zhou

6,0751 gold badge47 silver badges55 bronze badges

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.

answered Aug 20, 2020 at 15:37

Prabhat Ranjan's user avatar

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.

answered Mar 10 at 14:00

asti205's user avatar

1

Did you remember to include the closing brace in main?

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

using namespace std;

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

answered Jul 5, 2013 at 6:08

Norwæ's user avatar

NorwæNorwæ

1,5458 silver badges18 bronze badges

1

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

answered Jul 5, 2013 at 8:22

Stefano Falasca's user avatar

Stefano FalascaStefano Falasca

8,7772 gold badges17 silver badges24 bronze badges

2

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…

answered Nov 24, 2014 at 22:40

normyp's user avatar

normypnormyp

1741 silver badge11 bronze badges

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.

answered Nov 20, 2015 at 12:07

vesperto's user avatar

vespertovesperto

7641 gold badge5 silver badges23 bronze badges

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.

answered Feb 27, 2020 at 7:36

Mufassir Chowdhury's user avatar

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.

answered Aug 24, 2020 at 9:30

minTwin's user avatar

minTwinminTwin

1,1212 gold badges19 silver badges33 bronze badges

Arduino Forum

Loading

Solution 1

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.

Solution 2

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.

Solution 3

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

Related videos on Youtube

'class hardwareserial' has no member named 'printin'; did you mean 'println' ?Arduino programming

00 : 29

‘class hardwareserial’ has no member named ‘printin’; did you mean ‘println’ ?Arduino programming

'class TimeAlarm class' has no member named 'alarm repeat'; did you mean 'alarmRepeat' ? Arduino

00 : 42

‘class TimeAlarm class’ has no member named ‘alarm repeat’; did you mean ‘alarmRepeat’ ? Arduino

iOS : XCode 6 Framework issue: Module [framework] has no member named [class]

01 : 12

iOS : XCode 6 Framework issue: Module [framework] has no member named [class]

Class Usb has no member named ctrl_Req usb HOST Fix Bypass A5 devices

15 : 44

Class Usb has no member named ctrl_Req usb HOST Fix Bypass A5 devices

'class MPU6050' has no member named 'begin' error and solution!

03 : 07

‘class MPU6050’ has no member named ‘begin’ error and solution!

class TMRpcm' has no member named 'startRecording' error, arduino uno, nano and lower

03 : 06

class TMRpcm’ has no member named ‘startRecording’ error, arduino uno, nano and lower

Comments

  • 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’

    • The code looks fine. Maybe it’s trying to use an older version of the header.

    • Sometimes a «Rebuild All» fix everything.

    • Just tried it, getting the same error :/

    • You should post some code that reproduces the problem. The code you posted looks fine.

    • 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

    • 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 <other-includes-etc>» to get proprocessor-stage output that will show if the header you expect is being incorporated properly in the translation unit.

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

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

    • 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

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

    • what windows version do you use?

    • I am currently using Windows 7

    • well goto start -> run -> type cmd.exe and enter then follow my previous comment.

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

    • so find mingw32-g++.exe and use its absolute path to call (like c:somefoldermingw32-g++.exe main.cpp class.cpp -o main.exe)

    • 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!

    • 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

  • Yes that is there, I just accidentally left it out of the post.

  • The toolchain I’m using is mingw32-g++.exe

  • If he «forgot to compile Class.cpp» that would give an undefined reference link error, not the compilation error shown

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

Recents

Related

  • Forum
  • General C++ Programming
  • Class has no member named function

Class has no member named function

Hi guys,
This error is really annoying me but its late and i’ve been programming all day so I might be missing something stupid.
I keep getting an error saying ui.h:30: error: ‘class BTree<Word>’ has no member named ‘prntInOrder’

here is my ui.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

#include "btree.h"

#include <fstream>
#include <iostream>
using namespace std;

class UI
{
	public:
		void go (string filename);
		void help ();
	private:
		BTree<Word> tree;
		
		bool load(string filename);
};

ui.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "ui.h"

void UI::help()
{
	cout << "::Binary Search Tree::" << endl;
	cout << endl;
	cout << "Usage: Tree <filename>" << endl;
}

void UI::go(string filename)
{
	if (!load(filename))
		return;
	tree.printInOrder();
	
}

and part of my btree.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once

#include "node.h"

#include <iostream>
using namespace std;

template<class T>
class BTree
{
	public:
		
		BTree (): root(NULL){};
		~BTree ()
		{
			clear(root);
		}
		void printInOrder()
		{
			count = 1;
			printInOrder(root);
		}
		
	private:
		
		Node<T>* root;
		
		void printInOrder(Node<T>* node)
		{
			printInOrder(node->getLeft());
			cout << node->getData() << endl;
			printInOrder(node->getRight());
		}
};

As you can see the printInOrder() function is there so would it not see it?

You are calling:
tree.printInOrder(); (no parameters)

Your function is:
void printInOrder(Node<T>* node) (1 parameter)

You need to give it a parameter.

EDIT: crap, just saw the other overload, nevermind…

Last edited on

You should show the whole error message and the place in your code where the function is called.

ui.h: In member function ‘void UI::go(std::string)’:
ui.h:30: error: ‘class BTree<Word>’ has no member named ‘printInOrder’

ui.h:30 doesnt exist but the place that im calling printInOrder is line 14 in ui.cpp

What is tree in this code?

void UI::go(string filename)
{
if (!load(filename))
return;
tree.printInOrder();

}

BTree<Word> tree;
as shown in line 15 of ui.h

I do not see the definition of tree. So your presented code is not adequate to the proiblem.

> ui.h: In member function ‘void UI::go(std::string)’:
> ui.h:30: error: ‘class BTree<Word>’ has no member named ‘printInOrder’
> ui.h:30 doesnt exist …

Do uou have more than one file ui.h?

Are you using an IDE of some kind, and compiling the code from within the IDE?

If so, perhaps the files that you think are being compiled are not the files that are actually compiled.

Im compiling with g++ on the console using a makefile and I only have 1 ui.h

Makefile

1
2
3
4
5
6
7
8
9
10
11
Tree.exe: word.o ui.o CinReader.o node.h btree.h driver.cpp
	g++ ui.o word.o CinReader.o driver.cpp -o Tree.exe
	
word.o: word.h word.cpp
	g++ -c word.cpp

ui.o: ui.h ui.cpp
	g++ -c ui.cpp 
	
CinReader.o: CinReader.h CinReader.cpp
	g++ -c CinReader.cpp

The line 30 (which doesn’t exist) may come from your template class, the compiler generates the class after the template and in this case the generated class is probably placed into your ui.h file (since there is where you use the template, line 15). Have you tested the template separately from ui.h?
Can we also see your node specification?

node.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Node 
 * Binary tree node
 * 
 * Author: Bianca Migueis
 * Created: 05/07/12
 * Edited: 5/18/12
 *
 */
#pragma once

#include "word.h"

template<class T>
class Node
{
	public:
		/**
		 * Overloaded constructor
		 * sets the data to newData and left and right children to NULL
		 * @param T* new data to create the object
		 */
		Node<T> (T* newData):data(newData), leftChild(NULL), rightChild(NULL){}
		
		/**
		 * Gets the data
		 * @returns T* pointer to data inside the object
		 */
		T* getData ()
		{
			return data;
		}
		
		/**
		 * Set left child
		 * @param Node* new left node
		 */
		void setLeft (Node* newLeft)
		{
			leftChild = newLeft;
		}
		
		/**
		 * Set right child
		 * @param Node* new right node
		 */
		void setRight (Node* newRight)
		{
			rightChild = newRight;
		}
		
		/**
		 * Get Left child
		 * @returns Node<T>*& the left child
		 */
		Node<T>* getLeft () const
		{
			return leftChild;
		}
		
		/**
		 * Get Left child
		 * @returns Node<T>*& the left child
		 */
		Node<T>*& getLeft ()
		{
			return leftChild;
		}
		
		/**
		 * Get Right child
		 * @returns Node<T>*& the right child
		 */
		Node<T>* getRight () const
		{
			return rightChild;
		}
		
		/**
		 * Get Right child
		 * @returns Node<T>*& the right child
		 */
		Node<T>*& getRight ()
		{
			return rightChild;
		}
		
		/**
		 * Overloaded operator >
		 * @returns true if this->data is bigger than src.data, false if not
		 */
		bool operator> (const Node<T>& src)
		{
			return (data > src.data);
		}
		
		/**
		 * Overloaded operator <
		 * @returns true if this->data is smaller than src.data, false if not
		 */
		bool operator< (const Node<T>& src)
		{
			return (data < src.data);
		}
		
		/**
		 * Overloaded operator ==
		 * @returns true if this->data is equal to src.data, false if not
		 */
		bool operator== (const Node<T> src)
		{
			return (data == src.data);
		}
		
		/**
		 * Overloaded operator <<
		 * @returns ostream& formatted to print the node contents
		 */
		friend ostream& operator<< (ostream& outs, const Node<T>& src) 
		{
			outs << src.data;
			
			return outs;
		};
		
	private:
		
		T* data;
		Node* leftChild;
		Node* rightChild;
};

btree.h line 13. I don’t think that’s the issue, however there is a ; after the { } of the constructor.

Topic archived. No new replies allowed.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Pick a username
Email Address
Password

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account

Понравилась статья? Поделить с друзьями:
  • Claas axion 850 коды ошибок
  • Cl1 ошибка стиральная машина самсунг
  • Cl ошибка в стиральной машине lg сенсорный дисплей
  • Ck3 ошибка связи со steam
  • Ck3 exe ошибка при запуске приложения 0xc0000142