Access another class

Hi,

I'm a newbie in C++, and I would like to access a class from another object. Here's the code:


mainConsoleObj2.h
1
2
3
4
5
6
class OtherMe {
	  public:
	  		 int a;
		 	 void setA(int i); 
		   	 int getA();
	  };



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

class OtherMe {
	  public:
	   int a;
 	   void setA(int i)
 	   {
  	  	   a = i;	  	   
           }
		   
	 int getA()
	 {
		   return a;
         }	  
};



mainConsole.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>
#include <iostream>
#include "mainConsoleObj2.h"

int main(int argc, char *argv[])
{
 	
    OtherMe* oe = new OtherMe();
    
    oe.setA(30);
    cout << "VALUE: " << oe.getA() << endl;
}


When I try to compile, I get the error:
setA has not been declared.

What's wrong?

Thanks
first of all
 
    oe.setA(30);

this is wrong, because you decleared oe as a pointer. You should change it
 
oe->setA( 30 );

Another mistake is that , you define the same class twice. You have to change your code.

mainConsoleObj2.cpp
1
2
3
4
5
6
7
8
void OtherMe::SetA( int i ) 
{
      a = i ;
}
int OtherMe::getA()
{
     return a ;
}

Last edited on
Topic archived. No new replies allowed.