Reputation: 531
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;
}
Upvotes: 1
Views: 5347
Reputation: 56921
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:
using namespace std;
- get rid of it and add std::
where necessaryUpvotes: 3
Reputation: 1870
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();
Upvotes: 3
Reputation: 1500
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();
Upvotes: 3