Reputation: 837
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
Upvotes: 40
Views: 213150
Reputation: 6914
When you want to convert your code to result( executable, library or whatever ), there is 2 steps:
1) compile
2) link
In first step compiler should now about some things like sizeof objects that used by you, prototype of functions and maybe inheritance. on the other hand linker want to find implementation of functions and global variables in your code.
Now when you use ClassTwo
in File1.cpp
compiler know nothing about it and don't know how much memory should allocate for it or for example witch members it have or is it a class and enum or even a typedef of int, so compilation will be failed by the compiler. adding File2.cpp
solve the problem of linker that look for implementation but the compiler is still unhappy, because it know nothing about your type.
So remember, in compile phase you always work with just one file( and of course files that included by that one file ) and in link phase you need multiple files that contain implementations. and since C/C++ are statically typed and they allow their identifier to work for many purposes( definition, typedef, enum class, ... ) so you should always identify you identifier to the compiler and then use it and as a rule compiler should always know size of your variable!!
Upvotes: 1
Reputation: 206528
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.
Upvotes: 90
Reputation: 12087
you need to forward declare the name of the class if you don't want a header:
class ClassTwo;
Important: This only works in some cases, see Als's answer for more information..
Upvotes: 2
Reputation: 495
The thing with compiling two .cpp files at the same time, it doesnt't mean they "know" about eachother. You will have to create a file, the "tells" your File1.cpp, there actually are functions and classes like ClassTwo. This file is called header-file and often doesn't include any executable code. (There are exception, e.g. for inline functions, but forget them at first) They serve a declarative need, just for telling, which functions are available.
When you have your File2.cpp
and include it into your File1.cpp
, you see a small problem:
There is the same code twice: One in the File1.cpp
and one in it's origin, File2.cpp
.
Therefore you should create a header file, like File1.hpp
or File1.h
(other names are possible, but this is simply standard). It works like the following:
//File1.cpp
void SomeFunc(char c) //Definition aka Implementation
{
//do some stuff
}
//File1.hpp
void SomeFunc(char c); //Declaration aka Prototype
And for a matter of clean code you might add the following to the top of File1.cpp
:
#include "File1.hpp"
And the following, surrounding File1.hpp
's code:
#ifndef FILE1.HPP_INCLUDED
#define FILE1.HPP_INCLUDED
//
//All your declarative code
//
#endif
This makes your header-file cleaner, regarding to duplicate code.
Upvotes: 3
Reputation: 5246
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;
}
Upvotes: 20