how to separate a class code to different files?

K

key9

Hi all
I wrote:

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class BaseNode
{
public:
void return_message(string );
};

void BaseNode::return_message(string str)
{
std::cout << str;
}

int main(int argc, char *argv[])
{
string a = "dasdasd";
BaseNode test;
test.return_message(a);

system("PAUSE");
return EXIT_SUCCESS;
}

// ok this can pass compile but when I want to separated code to .h files:
//******************* main.cpp
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

#include "BaseNode.h

int main(int argc, char *argv[])
{
string a = "dasdasd";
BaseNode test;
test.return_message(a);

system("PAUSE");
return EXIT_SUCCESS;
}

//******************* basenode.h
#ifndef BASENODE_H_
#define BASENODE_H_

class BaseNode
{
public:
void return_message(string );
};

#endif //BASENODE_H_

//******************* basenode.cpp
#include <string>
#include <iostream>
using namespace std;

void BaseNode::return_message(string str)
{
std::cout << str;
}

//this can not pass compile : 5 D:\a\testclass\BaseNode.cpp `BaseNode' has
not been declared

this confuse exist on my other post on compile ,cause undefined reference to
foo::foo() but when I move foo's implement to .h file , error gone.

so how to organize a class's .h and .cpp file?
 
V

Victor Bazarov

key9 said:
[...]
//******************* basenode.cpp
#include <string>
#include <iostream>
using namespace std;

Add here:

#include "basenode.h"
void BaseNode::return_message(string str)
{
std::cout << str;
}

//this can not pass compile : 5 D:\a\testclass\BaseNode.cpp
`BaseNode' has not been declared

this confuse exist on my other post on compile ,cause undefined
reference to foo::foo() but when I move foo's implement to .h file ,
error gone.
so how to organize a class's .h and .cpp file?

You're on the right track. Keep going.

V
 
J

Jim Langston

key9 said:
Hi all
I wrote:

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class BaseNode
{
public:
void return_message(string );
};

void BaseNode::return_message(string str)
{
std::cout << str;
}

int main(int argc, char *argv[])
{
string a = "dasdasd";
BaseNode test;
test.return_message(a);

system("PAUSE");
return EXIT_SUCCESS;
}

// ok this can pass compile but when I want to separated code to .h
files:
//******************* main.cpp
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

#include "BaseNode.h

int main(int argc, char *argv[])
{
string a = "dasdasd";
BaseNode test;
test.return_message(a);

system("PAUSE");
return EXIT_SUCCESS;
}

//******************* basenode.h
#ifndef BASENODE_H_
#define BASENODE_H_

class BaseNode
{
public:
void return_message(string );
};

#endif //BASENODE_H_

//******************* basenode.cpp

#include "basenode.h"
#include <string>
#include <iostream>
using namespace std;

This is EVIL in a header. There are times using namespace std can cause
problems. If it's in a .cpp file at least you can see it and fix it. In a
header you have to search for it. Better would be
using std::cout
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top