How to work with class members?

I have the following code to test my understanding of headers and source files:

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TESTABLE_H_
#define TESTABLE_H_

class Testable{
public:
	Testable();
	~Testable();
	void someMethod();
	bool isConnected;
	char data;
};



#endif /* TESTABLE_H_ */


Source:
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
#include "Testable.h"
#include <iostream>

using namespace std;

Testable::Testable(){
	isConnected = true;
	data = 'a';
}

Testable::~Testable(){
	isConnected = false;
}

void Testable::someMethod(){
	data = 'b';
}

int main(){
	Testable objectA;
	cout << objectA.data;
	
	return 0;
}


However, in the source file, data is not being recognized as a member and I am given "Symbol 'data' could not be resolved" for those 3 instances of data. What am I not understanding?
Last edited on
I don't quite understand the error message you describe.

However, you have twice defined the constructor at lines 6 and 11
Testable::Testable()

but the definition for the destructor is missing
Testable::~Testable() { }
Last edited on
Oops, minor typo.

I still cannot access data from within the methods in the source file on lines 8, 16, or 21.

When a variable is declared in the class, shouldn't it be accessible by all the class's methods?
There is no problem with the code you have posted.
There is no problem with the code you have posted.


I forgot to save Testable.h after adding char data;.

I also forgot to add delete objectA; after the cout.

Thanks!
Last edited on
Are there any other #includes or other code which might have the name 'data'?
Perhaps there's some naming conflict due to using namespace std;
Try removing that line and explicitly put std::cout
delete is used to delete objects allocated with new. delete objectA; shouldn't even compile because objectA is not a pointer. objectA will be deleted automatically when it goes out of scope.
Last edited on
delete is used to delete objects allocated with new. delete objectA; shouldn't even compile because objectA is not a pointer. objectA will be deleted automatically when it goes out of scope.


If objectA is automatically deleted when it goes out of scope, instantiating via
Testable objectA;
why is
Testable *objectA = new Testable;
not automatically deleted when out of scope?
why is
Testable *objectA = new Testable;
not automatically deleted when out of scope?

The pointer objectA will be automatically deleted. But the object that it is pointing to will remain, unless it is explicitly deleted.

The pointer is automatically deleted? I thought that once you delete the pointer to the dynamic object it automatically calls the destructor for that class?
Maybe a misuse of terminology.

take this example:

1
2
3
4
5
6
7
    {
         int x = 1;
         int * p = new int;
    }
    // at this point, both x and p have gone out of scope and no longer exist.
    // but the integer pointed to by p still exists. Though we have no way
    // to access it now  
When you do delete objectA; it doesn't delete the pointer. It deletes what the pointer points to.
Topic archived. No new replies allowed.