User forums > Using Code::Blocks
Code Blocks, show linked classes
Andrew Cay:
So today I decided to teach my self how to link seperate c++ files into the "main.cpp" file through a "header.h" file.
I got it to work after a couple tries, but it's hard to program on code::blocks right now since when I'm on the main.cpp it doesn't show a list of options from any of the linked .cpp files.
IE:
* I have a test class in file "extras.cpp" named "simple_math"
* In main.cpp I will start by typing "simple_" and by that time I'd expect Code::Blocks to give me a drop down list with "simple_math" - but that doesn't happen.
* These drop down methods only happen when a function, or class, or class method are located in the same file.
Any one know how to enable an option that allows for this? Or if I have to just "know" all my class names?
golgepapaz:
Well ,You are not supposed to see it anyway.
From your "main.cpp" file, you need to include the header file that declares the class simple_math.
which is by convention ends with the extension ".h" , ".hpp" or some other variation thereof . (This is "extras.h" in your case.)
Don't include ".cpp" files from other files unless you know what you are doing.
You don't have the define the class and its methods in the header file .You can declare them in the
header file and provide the definitions in the source file. (that is "extras.cpp" in your case)
The linker will do the rest..
Andrew Cay:
This is what my header file looks like exactly line for line
--- Code: ---#include "classLib.cpp"
--- End code ---
This is how my header is included in my main.cpp
--- Code: ---#include "header.h" // this file links any other .cpp files
--- End code ---
--- Quote ---you don't have the define the class and its methods in the header file .You can declare them in the
header file and provide the definitions in the source file.
--- End quote ---
What do you mean? I have all my code in the c plus plus file, and I have that file "#include" 'd inside the header file, which is then linked into the main.cpp file.
This is my second day coding in c++, so I'm still familiarizing myself with the differences between "definitions" and "declarations" of classes.
Declaring a class is merely writing "class className {} " and defining them is making a prototype? I'm not sure.
Any ways. I was having no problems, everything is linked in. BUT I am not getting a drop down list of completions for methods, functions, and classes from other files that are linked to the main.cpp .
--- Quote ---Don't include ".cpp" files from other files unless you know what you are doing.
--- End quote ---
I have no idea what you are talking about. Why wouldn't I make ".cpp" files if I need that to make c plus plus files. All I'm doing is "#include " plus the file name with it's extension into the header only, and it links.
stahta01:
You need to learn the proper way to compile multiple source files!
Edit: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044842972&id=1043284392
Tim S.
Andrew Cay:
Thanks for the link! I think I'm linking multiple sources together correctly now.
Here's my header :
--- Code: ---#ifndef header_h
#define header_h
class math{
public:
int add( int, int);
int subtract( int, int);
};
#endif // math
--- End code ---
and here's the corresponding .cpp file
--- Code: ---#include "header.h"
int math::add( int x, int y){
return (x + y);
};
int math::subtract( int x, int y){
return( x - y );
};
--- End code ---
Then lastly, how I link it into the main.cpp file
--- Code: ---#include "header.h"
--- End code ---
I'm sure this is much better then what I was previously doing, and it works exactly the same. But sadly, I'm still not seeing any drop down lists for completion in the Code::Blocks ide. I am doing something wrong? Or is there an option that can fix this?
Navigation
[0] Message Index
[#] Next page
Go to full version