473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to separate a class code to different files?

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?




May 24 '06 #1
3 2130
key9 wrote:
[...]
//******************* 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
--
Please remove capital As from my address when replying by mail
May 24 '06 #2
* key9:
so how to organize a class's .h and .cpp file?


See <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>,
chapter 1.6.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 24 '06 #3

"key9" <ia*****@126.com> wrote in message
news:e5**********@news.yaako.com...
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

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?



May 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nathan Lamont | last post by:
I would like to make use of two separate PHP applications which happen to share some class names; trying to include both from one script results in the expected error, "cannot redefine class foo."...
1
by: Paul | last post by:
Title: What are the Consequences of Aspx page separate from app DLL Hi JL; I am working on a big asp.net application. When we migrate the dll (or dlls) to the production server, all users who are...
1
by: Newsgroup - Ann | last post by:
Hi gurus, I have two files compiled together. One file has the following global definition: char s = "asdf"; Another file has the following declaration: extern char *s;
4
by: marcelf3 | last post by:
Hello, I am a novice/moderate html programmer and I wonder how to tell the browser from which start point and to which end point in the text will each page be printed. In other words, I have a...
6
by: Pete Davis | last post by:
I'm confused about what precisely the limitations are on loading plugins in separate app domains. In all my previous apps that supported plugins, I've loaded them into the same domain as the app,...
7
by: jsale | last post by:
I have made an ASP.NET web application that connects to SQL Server, reading and writing data using classes. I was recommended to use session objects to store the data per user, because each user...
6
by: mister.mwa | last post by:
Hello, I have the following problem: I have a class Foo, and a class Bar. I want Foo to inherit from Bar, but i want to put them in separate files Foo.vb and Bar.vb. Then i will use the Foo...
4
by: Humberto Alvarez | last post by:
Hi As you might know, some asp.net host companies don't like dll in their servers, so one has to upload the source files, right? Well, the problem is that I have class libraries (.vb files)...
3
by: LurfysMa | last post by:
I would like to hear opinions on the tradeoffs of putting the tables, forms, and queries for several related datasets in separate databases vs one combined database. I am working on an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.