Make an instance of a external class

Hi,

I'm used to program in Java and I started to program in c++. Now in java if you have 2 classes (Class1.java and Class2.java) you could say in Class1: mClass2 = new Class2();

How do you do that in C++, I tried the following:

1
2
3
4
5
6
  	Class2 *mCLass2;

        and         

        Class2 mClass2;


But nothing works. The classes are doing nothing yet, I just want to make an instance of an external class. HOw do I do this in c++


The classes:
1
2
3
4
5
6
7
8
9
10
public class Class1
{
Class2 *mCLass2;

public:Class1()
	   {
		   
	   }
};

1
2
3
4
5
6
7
8
9
public class Class2
{

public:Class2()
	   {
		   
	   }
};
Class2 mClass2;

That line will create an instance of Class2. Why do you say it doesn't work? What behaviour are you seeing that makes you think it's not working?

Class2 *mCLass2;

That line just creates a pointer to an object of type Class2. It doesn't create an instance of the class.
Last edited on
Because visualstudio gives this error:

1
2
3
1>CLass1.cpp(3): error C2146: syntax error : missing ';' before identifier 'mClass2'
1>Class1.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Class1.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Then why not tell us that upfront? If you want people to help, it's in your interest to provide us with as much information as possible.

Without seeing your code, it's impossible to be sure what's causing the problem, but my guess would be that you've forgotten to include the definition of Class2 in the code that's trying to instantiate an object of the class.
Yea, that was not so smart on my part. sorry. WIll post the errors in the future.

The class code is the code posted above, the class 1 and class 2. So shouldnt it be enolugh to say Class2 mClass2; like in java ?

Or do I need to something extra in C++
public class Class1

There is no "public class" in C++

Or do I need to something extra in C++

you need to include the second file
In java you'd have to include a package if Class2 were in a different package, in c++ there are no packages and so you need to include the header files you need.

1
2
3
4
5
6
#ifndef CLASS2_H
#define CLASS2_H

class Class2 {};

#endif // CLASS2_H 


1
2
3
4
5
6
7
8
9
10
11
12
#ifndef CLASS1_H
#define CLASS1_H

#include "Class2.h"

class Class1 
{
public:
    Class2 mClass2;
};

#endif // CLASS1_H 



if both classes are in 1 file make sure to define Class2 before Class1
1
2
3
4
5
6
7
class Class2{};

class Class1 
{
public:
    Class2 mClass2;
};
Last edited on
Ok thnx :D is it also possible to do #ifndef CLASS1_H,CLASS2_H ? so multiple classes or is it better if I than make a kind of loader class?
@Zeepblok

It sounds like you really need to go back to the very basics of C++, and get some understanding of the rudiments of the language. In order for the compiler to understand what "Class2" means, you have to include the definition of that type. The conventional way to do this is as Gamer2015 has shown - by putting the class definition into a header file, and having every translation unit that needs the definition, include the header file.
Last edited on
Ok, back to the basics it is. Thnx for the help :)
The #ifndef CLASS2_H is an include gurad:

http://en.wikipedia.org/wiki/Include_guard


#include is similar to import in java. In c/c++ you inlcude the file header not a specific class. There could be more in this file header than just one class.
Topic archived. No new replies allowed.