Author Topic: Creating a new class file  (Read 31860 times)

Offline barliesque

  • Single posting newcomer
  • *
  • Posts: 3
Creating a new class file
« on: December 25, 2010, 04:24:30 am »

Hi, I'm new to Code::Blocks, using it to learn C++.  I'm having a problem creating new class files and adding them to my project.  I begin with a console project, and then create a new class file.  The class wizard creates a .cpp and .h pair, but the include in the .cpp file...
Code
#include "Rectangle.h"

Rectangle::Rectangle()
{
    //ctor
}

Rectangle::~Rectangle()
{
    //dtor
}

...produces the following error when I build the project:
Quote
C:\Users\Barliesque\Documents\C++\First Project\src\Rectangle.cpp|1|error: Headers/include/Rectangle.h: No such file or directory

...despite both Rectangle.cpp and Rectangle.h being listed in the project build targets.  So, what do I gotta do?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Creating a new class file
« Reply #1 on: December 25, 2010, 07:57:15 am »
If you create the new class, you decide in which directories you want to have the sources and headers installed.

C::B does not add the header directorie(s) to the compilers search dirs automatically.

You have to add it manually in "Project -> Build options -> [project and/or target] -> Search directories -> Compiler".

Offline barliesque

  • Single posting newcomer
  • *
  • Posts: 3
Re: Creating a new class file
« Reply #2 on: December 26, 2010, 07:33:42 am »
Thanks for the reply.  That's done it.   :)

Seems kind of strange to me that the folders would be automatically created by Code::Blocks, but not automatically added to the compiler's search paths.

So, the new class files were listed under "Build Targets" but were invisible to the compiler.  (?!)  Requiring these files to be listed in both places seems redundant to me.  Why should both lists be required?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Creating a new class file
« Reply #3 on: December 26, 2010, 09:12:57 am »
Thanks for the reply.  That's done it.   :)

Seems kind of strange to me that the folders would be automatically created by Code::Blocks, but not automatically added to the compiler's search paths.

So, the new class files were listed under "Build Targets" but were invisible to the compiler.  (?!)  Requiring these files to be listed in both places seems redundant to me.  Why should both lists be required?
If you create the new class you can chose where to create it and whether header and source should be in the same folder or not.
It depends on you how to include a file, sometimes it makes more sense to use (parts of) the path in the include statement (like for many sdk headers "wx/...", "boost/...") or if you have header-files with the same name in different folders.
It's not easy to find a solution that fits all needs, so I think adding the include-dir explicitely gives you more freedom then a solution, that does it automagically.

Offline barliesque

  • Single posting newcomer
  • *
  • Posts: 3
Re: Creating a new class file
« Reply #4 on: December 26, 2010, 08:16:10 pm »
Oh sure, I agree, flexibility for individual development styles and situations is important.

I'm not sure I made my question quite clear, though:  Why are there two different places in the application where source file locations may be specified?  ie,  Project>>Properties>>Build Targets  as well as Project>>Build Options>>Search Directories  ...I expect I'm just not understanding the difference between these two dialogs, but they do seem like two things that ought to be unified into a single dialog.

By the way, I did try including full paths in my include statements (relative to main.cpp in the root folder of my project) but they still were not found (until I added src and include) to the Search Directories.
« Last Edit: December 26, 2010, 08:19:35 pm by barliesque »