Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding C++ Include another class

Tags:

c++

I have two files:

File1.cpp File2.cpp 

File1 is my main class which has the main method, File2.cpp has a class call ClassTwo and I want to create an object of ClassTwo in my File1.cpp

I compile them together by

g++ -o myfile File1.cpp File2.cpp 

but when I try to create by

//create class two object

ClassTwo ctwo; 

It doesn't work.

Error was

ClassTwo was not declared in this scope.

This is my main.cpp

#include <iostream> #include <string> using namespace std;  int main() { //here compile error - undeclare ClassTwo in scope. ClassTwo ctwo;  //some codes } 

Here is my File2.cpp

#include <iostream> #include <string>  using namespace std;  class ClassTwo { private: string myType; public: void setType(string); string getType(); };   void ClassTwo::setType(string sType) { myType = sType; }  void ClassTwo::getType(float fVal) { return myType; } 

Got respond of splitting my File2.cpp into another .h file but if i am declaring a class, how do i split it into another .h file as i need to maintain the public and private of the variable(private) and functions(public) and how do i get ClassTwo ctwo to my File1.cpp at main method

like image 378
baoky chen Avatar asked Oct 04 '12 18:10

baoky chen


People also ask

Is it possible to have one class as a member variable of another class?

You need to make the variables in class aaa as class variables, and then you can use these variables of class aaa in class bbb by using object of class aaa. e.g. aaa obj2=new aaa();

Can a class contain another class in it in C++?

Nested Classes in C++ A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.

How do I use a class from another class in C++?

Through class conversion, one can assign data that belongs to a particular class type to an object that belongs to another class type. where '=' has been overloaded for objects of class type 'B'. Class conversion can be achieved by conversion function which is done by the use of operator overloading.


2 Answers

What is the basic problem in your code?

Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).
The compiler needs to see the composition of a type when you write something like

ClassTwo obj; 

This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo. The most common way to do this in C++ is to split your code in to header files and source files.
The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who's object they create.

Why can't I simply put all code in cpp files and include them in other files?

You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

How to solve this particular problem?

Your code should be organized as follows:

//File1.h

Define ClassOne  

//File2.h

#include <iostream> #include <string>   class ClassTwo { private:    string myType; public:    void setType(string);    std::string getType(); };  

//File1.cpp

#include"File1.h"  Implementation of ClassOne  

//File2.cpp

#include"File2.h"  void ClassTwo::setType(std::string sType) {     myType = sType; }  void ClassTwo::getType(float fVal) {     return myType; }  

//main.cpp

#include <iostream> #include <string> #include "file1.h" #include "file2.h" using namespace std;  int main() {      ClassOne cone;     ClassTwo ctwo;      //some codes } 

Is there any alternative means rather than including header files?

If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

like image 59
Alok Save Avatar answered Sep 20 '22 23:09

Alok Save


C++ (and C for that matter) split the "declaration" and the "implementation" of types, functions and classes. You should "declare" the classes you need in a header-file (.h or .hpp), and put the corresponding implementation in a .cpp-file. Then, when you wish to use (access) a class somewhere, you #include the corresponding headerfile.

Example

ClassOne.hpp:

class ClassOne { public:   ClassOne(); // note, no function body           int method(); // no body here either private:   int member; }; 

ClassOne.cpp:

#include "ClassOne.hpp"  // implementation of constructor ClassOne::ClassOne()  :member(0) {}  // implementation of "method" int ClassOne::method() {   return member++; } 

main.cpp:

#include "ClassOne.hpp" // Bring the ClassOne declaration into "view" of the compiler  int main(int argc, char* argv[]) {   ClassOne c1;   c1.method();    return 0; } 
like image 41
S.C. Madsen Avatar answered Sep 19 '22 23:09

S.C. Madsen