0

[EDIT:]

The problem seems to belong to the functions, that take default-parameters. Without separating in *.h *.cpp and main file it worked as i implemented something like:

void foo(double db;);                  // deklaration
void foo(double db = 4){ cout << db;}  // definition
int main(){
    foo();                             // usage
    return 1;
}

But if I separate deklaration (-> *.h), definition (-> *.cpp) and usage (-> main) compiling suddenly returns an erro telling, there is no function foo(void), as it does not recognize that there is a default parameter. Any suggestions for that?

[/EDIT]

I wrote a c++-program running somehow like:

#include <iostream>
/* other includes */

using namespace std;

class my_class
{
private:
    /* variables */
public:
    /* function deklarations (just some short ones are only defined not declared) */
};
ostream& operator<<(ostream &out, my_class member);

/* Definition of the member functions and of the not-member-function */

int main()
{
    /*some trial codes of member-functions */
    return 1;
}

In one total file all compiled well in Eclipse and worked. Now I also wanted to try seperate in a main,class-header and class-cpp file (called them "my_class.h" and my_class.cpp").

For that i put in class-header:

#ifndef MY_CLASS_H_
#define MY_CLASS_H_
#include <iostream>
/* other includes */

using namespace std;

class my_class
{
    /* ... */
};
ostream & operator<<(ostream &out, my_class member);
#endif /* MY_CLASS_H_ */

I put in class-cpp:

/* Definition of the member functions and of the not-member-function */

I put in main:

#include <iostream>
#include "my_class.h"
#include "my_class.cpp"

int main()
{
    /*some trial codes of member-functions */
    return 1;
}

This version is compiling with the g++ command in commandline:

g++ -o main.exe main.cpp

But it does not Compile in Eclipse. There it gives me the Error:

...\my_class.cpp:11.1: error: 'my_class' does not name a type

and same for all other member functions and variables. I tried to follow the instructions from here (I put just "my_class.h" in main and my_class.cpp, but then it did not compile in Eclipse and in command line (of course then with the my_class.cpp included). Eclipse gives me an Error, that makes me believe Eclipse does not see the "my_class.cpp":

...\main.cpp:288:47: error: no matching function for call to 'my_class::foo(...)'

where foo stands for the first member-function declard in the "my_class.cpp" file. First It gave the error for the constructor too, but as I put it's definition directly into the *.h file it worked well. (That's why I think, it does not see the "my_class.cpp" file)

I think I might be missing something very trivial as I am very new to Eclipse, but I don't see it. I tried to make my questions and information as short as possible.

Martin
  • 493
  • 6
  • 16
  • Learn how to use a builder like `make` and always pass `-Wall` (perhaps with `-g`) to `g++`; I believe that you might not need `eclipse` at all for C++ programs (which you could edit with e.g. `emacs`) – Basile Starynkevitch Aug 13 '13 at 13:46
  • 3
    You shouldnot `#include "my_class.cpp"` anywhere. – juanchopanza Aug 13 '13 at 13:50
  • @Basile Eclipse automatically uses -Wall -g and thanks for your suggestion, but I'd prefer to try out an IDE for now... up to it was just me and Editor and the compiler – Martin Aug 13 '13 at 13:51
  • @ juanchopanza: I know, but - as I said - it is the only way it compiles at the time (at least with g++), that's exactly my problem ;) – Martin Aug 13 '13 at 13:52
  • I use CMake and Eclipse. See: http://www.vtk.org/Wiki/CMake:Eclipse_UNIX_Tutorial –  Aug 13 '13 at 13:58
  • There should be a possibility in Eclipse CDT to add a new class to project or add existing code to the project. This way Eclipse would create a proper makefile for you. Never include the implementation (C/CPP) files! – Valentin H Aug 13 '13 at 14:16
  • @ Valentin: So you mean including a separate .h and a separate .cpp file (that's what I did) will work different then let them create as a new c++class by the built in function? – Martin Aug 13 '13 at 15:25
  • I am not quite sure what to do... shall I now answer my own question or not? On the one hand it will show the answer, it will show the question answered and it is a better syntax, on the other hand my true problem was already solved twice, which from the very beginning I didn't know to be familiar with my problem. :S – Martin Aug 14 '13 at 08:02

1 Answers1

0

default-parameters need to be declared in the header-file as it contains the declarations and not in the cpp file, which contains the definitions. (An additional mistake was to declare them in the definition). Found some help here. But why did it work, as I implemented it in one whole file?

Answer:

If default-parameter is in the cpp-file, the main file does not see it as
it looks only into the header-file
But if the whole code is included in just one file, the default-value
can be found in the definition too.

To explain myself:

I considered answering my question, because it gives a better overview of the whole question and the question will now not appear as unanswered. After reading this, I think that it is the right way to do so.

Community
  • 1
  • 1
Martin
  • 493
  • 6
  • 16