• Announcements

Archived

This topic is now archived and is closed to further replies.

Recommended Posts

1-Sometimes when i compile a project, I get an error that reports a copy of the funtion is already in the obj file. To fix this I take the Funtions body out of the header file and into a cpp file. Example: class1.h
class class1 {
class1();
}
class1::class1() {

}

I take out class1::class1() { } and put it in a new class1.cpp If I change *.h to *.hpp would this fix it? and why does this happen randomly? Does it have smoething todo with Precompiled headers? 2-If I made alot of small tools, File parsers, containers, whatever. Can I just place it all in a header file, so its an easy interface to other projects with a simple #include. 3-Any others random facts on header files you want to hit me with?

Share on other sites
1 - No, changing it to a .hpp file will not help.

At a guess the reason it''s saying that the function is already defined is because it''s in the header. I''m guessing that your header file is included in more than one place in your code.

To remedy this problem, put this code around your code in the header file. Don''t forget to change the _CLASS1_H_ for different header files.

#ifndef _CLASS1_H_
#define _CLASS1_H_

blah blah blah

.. end of your code here
#endif // !_CLASS1_H_

This would help with issue 2. Personally, i would create my own set of small libs to include for this.

As a rule i don''t put function code into headers, only .cpp files, i find it''s easier to manage my code this way.

3 - instead of the #ifndef code above...you could also try the #pragma once at the top of your header files. It has pretty much the same effect.

Share on other sites
Though I didnt include it in my [*code] [*/code] above; I always define my header files.

I''ve put the function within the defined header, and outside the define, but still in the *.h.

This is not a new problem really, but I just was thinking about making some of my tools all header files.

As for the lib idea, I don''t disagree at all, except for small tools.

Share on other sites
"Can I just place it all in a header file, so its an easy interface to other projects with a simple #include."

A good way to do this is with the "pimpl" (pointer to implementation) idiom. You can find gobs of info on the net, but essentially you define your header like this:

class MyClass {
struct MyClassImpl * impl_;
public:
// public interface
};

And your .cpp file like this:

struct MyClassImpl {
// class data here
};

Your constructor allocates and intializes impl_, your destructor deletes it. Your member functions pass around the impl pointer and the implementation is hidden completely from users of the class, which is the desired goal.

I even avoid private member functions. I declare my worker functions to be static in the .cpp file and pass them a pointer to the impl if they need to modify class data.

This and other techniques found in Lakos'' /Large Scale C++ Software Design/ will greatly reduce code dependencies and compile times, especially on larger projects.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

• Partner Spotlight

• Forum Statistics

• Total Topics
627682
• Total Posts
2978622

• 9
• 14
• 12
• 10
• 12