473,385 Members | 1,752 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.

still problem of organize file

Hi all

on last post I confused on how to organize file of class, ok ,the problem
solved : should include class define head on cpp file.
but this time ,still link error:

strange is I put the implement to .h file directly like this:
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch){
fprintf(stdout,"%c",ch); <--- directly implement here
}
};

there's no error occur;

but when I wrote the .cpp file,(same name as .h file)
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch);
};

*******cpp file******
#include "LinuxTestTerminal.h"
void
LinuxTestTerminal::printch(char ch){
fprintf(stdout,"%c",ch);
}

error occurs: undefined reference of printch();
What's on earth the problem is?

thank you very much!

key9
completed code here : cygwin enviroment

// test.cpp
// use -lstdc++

#include <stdio.h>
#include <unistd.h>
#include <iostream>

#include "LinuxTerminal.h"

using namespace std;
//this printch function is just for test ,direct use this can pass the test
print
/* void printch(char ch){
fprintf(stdout,"%c",ch);
}
*/
int main(int argc, char *argv[])
{
char q = 'c';

printf("print a new line of doing test");
LinuxTestTerminal* lt = new LinuxTestTerminal();
lt->printch(q);

//printch(q); // direct use this can pass, but lt->printch(q) can not
,why?

while(true); // test perpose loop

}

***********************************
// Terminal.h

#ifndef __TERMINAL_H_
#define __TERMINAL_H_
class Terminal{ // this is virtual class of terminal
public:

/* <--- comment for test perpose since can not pass the complie
virtual void printch(char) = 0;
virtual void backSpace() = 0;
virtual void printCR() = 0;
virtual void printTab() = 0;
*/

};
#endif // __TERMINAL_H_

***********************************
// LinuxTerminal.h

#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:

void printch(char ch);
void backSpace();
void printCR();
void printTab();
};
#endif // __LINUXTERMINAL_H_

****************************************
// LinuxTerminal.cpp

#include "LinuxTestTerminal.h" // this time, add this line

void
LinuxTestTerminal::printch(char ch){
fprintf(stdout,"%c",ch);
}

void
LinuxTestTerminal::backSpace(){
fprintf(stdout,"/b")
}

void
LinuxTestTerminal::printCR(){
fprintf(stdout,"/n");
}

void
LinuxTestTerminal::printTab(){
fprintf(stdout,"/t");
}

May 24 '06 #1
2 1857
key9 wrote:

on last post I confused on how to organize file of class, ok ,the problem
solved : should include class define head on cpp file.
but this time ,still link error:

strange is I put the implement to .h file directly like this:
there's no error occur; error occurs: undefined reference of printch();


Are you sure you link all these files together? Check your compiler's
documentation to learn how to deal with multiple source files.
Jonathan

May 24 '06 #2

"key9" <ia*****@126.com> wrote in message
news:e5**********@news.yaako.com...
Hi all

on last post I confused on how to organize file of class, ok ,the problem
solved : should include class define head on cpp file.
The link problem was NOT because you failed to include the header file in
your source file. If that was the problem, then the .cpp file would fail to
compile, and you'd get a compiler (not link) error.
but this time ,still link error:

strange is I put the implement to .h file directly like this:
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch){
fprintf(stdout,"%c",ch); <--- directly implement here
}
};

there's no error occur;

but when I wrote the .cpp file,(same name as .h file)
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch);
};

*******cpp file******
#include "LinuxTestTerminal.h"
void
LinuxTestTerminal::printch(char ch){
fprintf(stdout,"%c",ch);
}

error occurs: undefined reference of printch();
What's on earth the problem is?

thank you very much!

key9


It's the same problem as before. It isn't your code that's the problem,
it's the way you're compiling and linking. When you are compiling and
linking, you're simply failing to include the .cpp file properly. (And when
I say "include", I am referring to making sure the .cpp file compiles and
links properly, NOT whether you're using the preprocessor symbol "#include"
correctly!) You probably just need to specify the .cpp filename somewhere
in the command line (or in your project settings, if you have such a thing).
Read your compiler's documentation on how to compile and link multiple
source files.

-Howard

May 24 '06 #3

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

Similar topics

10
by: Bruce W...1 | last post by:
I've been learning about PHP for a couple of weeks. With includes, PHP scripts, and HTML I can see where a large and complex website could easily turn in to a big hairy mess with files all over...
14
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing...
3
by: wapsiii | last post by:
Looking for better practise! In a large asp.net project, how is it best to organize classes? When should I create a new project for a class? Is it better to have fewer files with all the classes...
5
by: CMM | last post by:
I don't seem to "get" ASP.NET 2.0's Localization features. I've read up on everything... and of course, everything is explained in cursory softball terms- not any "real-world" usage way. I hope...
4
by: Daniel N | last post by:
I am new to .net and want to organize my code better. I am writing in vb.net and the code for my main form is nearing 50-60 pages and would like to create another file like a class, module or code...
12
by: Jchick | last post by:
Boy, this should be a simple bit of code but I can't figure out how to make it happen. I have a CSV file shows up in a directory that has 4 fields that need to be printed on labels. Each line of...
2
by: key9 | last post by:
Hi all look at the organize tree main.c ------ #include lib_adapter.c main() { foo();
6
by: Larry Bud | last post by:
Been working with .net 2 since January, and am in the middle of my first large project. It's becoming obvious that one must organize their code well in a large project to make maintenance...
2
by: Fredrik Lundh | last post by:
Dudeja, Rajat wrote: A Python program consists of a script file (the py file you run to start the program), and usually one or more additional module files (py files that you import). The...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.