Author Topic: Code Blocks, show linked classes  (Read 11472 times)

Offline Andrew Cay

  • Multiple posting newcomer
  • *
  • Posts: 10
Code Blocks, show linked classes
« on: July 19, 2013, 10:26:09 pm »
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?
« Last Edit: July 19, 2013, 10:29:11 pm by Andrew Cay »

Offline golgepapaz

  • Multiple posting newcomer
  • *
  • Posts: 44
Re: Code Blocks, show linked classes
« Reply #1 on: July 19, 2013, 11:20:56 pm »
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..

 

Offline Andrew Cay

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Blocks, show linked classes
« Reply #2 on: July 19, 2013, 11:43:19 pm »
This is what my header file looks like exactly line for line
Code
#include "classLib.cpp"

This is how my header is included in my main.cpp
Code
#include "header.h" // this file links any other .cpp files 

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.
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.
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.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Code Blocks, show linked classes
« Reply #3 on: July 20, 2013, 12:22:24 am »
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.
« Last Edit: July 20, 2013, 12:25:28 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Andrew Cay

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Blocks, show linked classes
« Reply #4 on: July 20, 2013, 12:57:37 am »
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

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 );
};

Then lastly, how I link it into the main.cpp file
Code
#include "header.h"

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?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Code Blocks, show linked classes
« Reply #5 on: July 20, 2013, 03:54:22 am »
I think "Settings" -> "Editor"
Select Code Completion from the list on left

I have no idea what you need to fix to get it to work; I have never used Code Completion (CC).

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Andrew Cay

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Code Blocks, show linked classes
« Reply #6 on: July 20, 2013, 03:59:15 am »
Okay, it was enabled the whole time... I clicked something, don't remember what. But now It's working for linked files. Thank's again Stahta!

Edit: Ahh! There was an option to enable "Enable headers code-completion" - very helpful.
« Last Edit: July 20, 2013, 04:02:48 am by Andrew Cay »