1

i'm trying to write my c++ program in different files, but I can't seem to get it to work. can someone help me?

separate.cpp

#include "separate.h"
#include <iostream>

void Separate() {

cout << "text";

}

separate.h

#include <string>
using namespace std;

class OneLine {

    Separate();

private:
    string vari;

};

main.cpp

#include "separate.cpp"
#include <iostream>

using namespace std;

int main () {

    Separate s;
    s();

return 0;
}
NewFile
  • 501
  • 5
  • 10
  • 16
  • 1
    How did this work in a single file?? You are declaring a function `Separate` of a class `OneLine`, but then try to create a `Separate` object. – Benjamin Bannier Sep 30 '13 at 14:38
  • Advice: Include approved headers first: #include than #include "separate.h" –  Sep 30 '13 at 14:39
  • 1
    You should avoid `using namespace std`, specially in headers. See [here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – juanchopanza Sep 30 '13 at 14:40
  • 2
    @DieterLücking: Why is `iostream` "approved"? If you include it first you'll never know if the header file (e.g. `separate.h`) is self-sufficient and doesn't rely on undeclared functions or types. Always include e.g. `separate.h` first. – Benjamin Bannier Sep 30 '13 at 14:42
  • Because your headers might introduce new overloads or macros which mess up the std headers. – Neil Kirk Sep 30 '13 at 14:57
  • @BenjaminBannier assume your header (included before) has just '{' in it - than an #include in g++ will result in "/usr/include/c++/4.7/exception:37:37: error: expected declaration before end of line" –  Sep 30 '13 at 15:20
  • @DieterLücking: That example is badly broken (or very special needs code) no matter where you include it and it is definitely an example of a header file which isn't selfy-sufficient. – Benjamin Bannier Sep 30 '13 at 18:24

3 Answers3

3

Two basic errors:

In separate.cpp, you need

void OneLine::Separate() { /*...*/ }

and in main.cpp you want to create an object of your type and call the defined method on it like this:

OneLine ol;
ol.Separate();

For this, you need to make the method public, change separate.h:

class OneLine {

public:
    Separate();

//...
};

You want to change a few more things as well which are not needed for this simple example but they will become necessary in the long run:

  • You want include guards, google for "include guard"
  • You don't want using namespace std; - get rid of it and add std:: where necessary
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
3

In your implementation define the function as:

void OneLine::Separate() {
  ...

In your main, you need to instantiate a OneLine object and call Separate on that, i.e.:

OneLine o;
o.Separate();
dornhege
  • 1,500
  • 8
  • 8
3

In your main file you need to reference "separate.h" rather than "separate.cpp"

In seperate.cpp the class method needs to be prefixed with the class name:

void Oneline::Separate()

Also you should be creating an object of type OneLine rather than of type Seperate.

So:

Oneline one;
one.Seperate();
Paddyd
  • 1,870
  • 16
  • 26