help please - newbie learning c++

J

Joao Serrachinha

I'm learning c++ and i can't build this example from a book:

//file GradeBook.h
#include <string>

using std::string;




class GradeBook

{

public:

GradeBook( string );

void setCourseName( string );

string getCourseName();

void displayMessage();

private:

string courseName;

};



//file GradeBook.cpp
#include <iostream>

using std::cout;

using std::endl;



#include "GradeBook.h"



GradeBook::GradeBook( string name )

{

setCourseName( name );

}




void GradeBook::setCourseName( string name )

{

courseName = name;

}




string GradeBook::getCourseName()

{

return courseName;

}




void GradeBook::displayMessage()

{


cout << "Welcome to the grade book for\n" << getCourseName()

<< "!" << endl;

}

//file fig003_13.cpp
#include "GradeBook.h"



#include <iostream>

using std::cout;

using std::endl;



int main()

{

GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );

GradeBook gradeBook2( "CS102 Data Structures in C++" );




cout << "gradeBook1 created for course: " <<
gradeBook1.getCourseName()

<< "\ngradeBook2 created for course: " <<
gradeBook2.getCourseName()

<< endl;

return 0;

}

I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:

g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

or

g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp:(.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp:(.text+0x13d): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp:(.text+0x17d): undefined reference to
`GradeBook::getCourseName()'
fig03_13.cpp:(.text+0x1ad): undefined reference to
`GradeBook::getCourseName()'
collect2: ld returned 1 exit status

Even if i include the preprocessor wrapper in the GradeBook.h file the
result is the same!
Why this happen, what's wrong!? i have a lot of samples that i can't
compile!

thanks in advance.
 
I

int2str

I'm learning c++ and i can't build this example from a book:

//file GradeBook.h
[code snipped]
//file GradeBook.cpp
[code snipped]
//file fig003_13.cpp
[code snipped]
I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:

g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

You are compiling and linking GradeBook.cpp, which does not contain a
main() function. The main function is in your fig003_13.cpp.

So use this command line to compile and link both CPP files:

g++ -W -Wall -o GradeBook GradeBook.cpp fig003_13.cpp

Cheers,
Andre
 
T

Thomas Matthews

Joao Serrachinha wrote:
[snip]
I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:

g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

or

g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp:(.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp:(.text+0x13d): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
fig03_13.cpp:(.text+0x17d): undefined reference to
`GradeBook::getCourseName()'
fig03_13.cpp:(.text+0x1ad): undefined reference to
`GradeBook::getCourseName()'
collect2: ld returned 1 exit status

Even if i include the preprocessor wrapper in the GradeBook.h file the
result is the same!
Why this happen, what's wrong!? i have a lot of samples that i can't
compile!

thanks in advance.

Your issue is not with the language but with the compiler.
You need to link all the files together into one executable:

g++-4.2 -Wall GradeBook.cpp figure03_13.cpp

The linking part is saying it can't find the references
because you haven't supplied them.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
N

Neelesh Bodas

I'm learning c++ and i can't build this example from a book:

//file GradeBook.h

[header file defines class GradeBook]
//file GradeBook.cpp

[ source file defines the member functions ]
//file fig003_13.cpp

[ this file uses the GradeBook class ]
I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:

g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

or

g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp:(.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
collect2: ld returned 1 exit status

<OffTopic>
The is because when you say g++ filename.cpp , the g++ command not
only invokes the compiler, but also the linker. The linker expects
definitions for all symbols used in the code, including definition for
function main(). The errors that you have quoted are all linker errors
and they say something to this effect - the definition of certain
symbols is not available.

What do you do to solve this issue?

a) separate out the compilation from linking. Look g++ documentation
on how to do this. A typical way could be:

// invoke only the compiler
g++ -c GradeBook.cpp -o GradeBook.o
g++ -c fig03_13.cpp -o fig03_13.o

//invoke the linker
g++ GradeBook.o fig03_13.0 -o result.cpp

b) also read about 'makefiles', something which helps tackling the
build process.
</OffTopic>

Observe that the errors you have quoted are all linker errors, hence
this is probably off-topic here.

-N
 
J

Joao Serrachinha

I'm learning c++ and i can't build this example from a book:
//file GradeBook.h

[header file defines class GradeBook]
//file GradeBook.cpp

[ source file defines the member functions ]
//file fig003_13.cpp

[ this file uses the GradeBook class ]


I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:
g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp:(.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
collect2: ld returned 1 exit status

<OffTopic>
The is because when you say g++ filename.cpp , the g++ command not
only invokes the compiler, but also the linker. The linker expects
definitions for all symbols used in the code, including definition for
function main(). The errors that you have quoted are all linker errors
and they say something to this effect - the definition of certain
symbols is not available.

What do you do to solve this issue?

a) separate out the compilation from linking. Look g++ documentation
on how to do this. A typical way could be:

// invoke only the compiler
g++ -c GradeBook.cpp -o GradeBook.o
g++ -c fig03_13.cpp -o fig03_13.o

//invoke the linker
g++ GradeBook.o fig03_13.0 -o result.cpp

b) also read about 'makefiles', something which helps tackling the
build process.
</OffTopic>

Observe that the errors you have quoted are all linker errors, hence
this is probably off-topic here.

-N


Thanks to all.

But why the file

//file fig003_13.cpp
#include "GradeBook.h"
change to
#include "GradeBook.cpp"
this way works fine.
Why not like this!?

thanks in advance.
 
J

Jim Langston

Joao Serrachinha said:
I'm learning c++ and i can't build this example from a book:
//file GradeBook.h

[header file defines class GradeBook]
//file GradeBook.cpp

[ source file defines the member functions ]
//file fig003_13.cpp

[ this file uses the GradeBook class ]


I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:
g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp:(.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
collect2: ld returned 1 exit status

<OffTopic>
The is because when you say g++ filename.cpp , the g++ command not
only invokes the compiler, but also the linker. The linker expects
definitions for all symbols used in the code, including definition for
function main(). The errors that you have quoted are all linker errors
and they say something to this effect - the definition of certain
symbols is not available.

What do you do to solve this issue?

a) separate out the compilation from linking. Look g++ documentation
on how to do this. A typical way could be:

// invoke only the compiler
g++ -c GradeBook.cpp -o GradeBook.o
g++ -c fig03_13.cpp -o fig03_13.o

//invoke the linker
g++ GradeBook.o fig03_13.0 -o result.cpp

b) also read about 'makefiles', something which helps tackling the
build process.
</OffTopic>

Observe that the errors you have quoted are all linker errors, hence
this is probably off-topic here.

-N


Thanks to all.

But why the file

//file fig003_13.cpp
#include "GradeBook.h"
change to
#include "GradeBook.cpp"
this way works fine.
Why not like this!?

thanks in advance.

Because by including the .cpp file you are including the source code which
gets compiled into one compilation unit (object) instead of 2.

But including a .cpp in a .cpp if a definate no no. You should put it back
the way it was and link the object files together.
 
J

Joao Serrachinha

I'm learning c++ and i can't build this example from a book:
//file GradeBook.h
[header file defines class GradeBook]
//file GradeBook.cpp
[ source file defines the member functions ]
//file fig003_13.cpp
[ this file uses the GradeBook class ]
I'm using g++ 4.2.1 and g++ 4.1.3 and the result is the same:
g++-4.2 -Wall GradeBook.cpp -o GradeBook
/usr/lib/gcc/i486-linux-gnu/4.2.1/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
or
g++ -Wall fig03_13.cpp -o fig03_13
/tmp/ccGH8FTv.o: In function `main':
fig03_13.cpp:(.text+0xb7): undefined reference to
`GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,
collect2: ld returned 1 exit status
<OffTopic>
The is because when you say g++ filename.cpp , the g++ command not
only invokes the compiler, but also the linker. The linker expects
definitions for all symbols used in the code, including definition for
function main(). The errors that you have quoted are all linker errors
and they say something to this effect - the definition of certain
symbols is not available.
What do you do to solve this issue?
a) separate out the compilation from linking. Look g++ documentation
on how to do this. A typical way could be:
// invoke only the compiler
g++ -c GradeBook.cpp -o GradeBook.o
g++ -c fig03_13.cpp -o fig03_13.o
//invoke the linker
g++ GradeBook.o fig03_13.0 -o result.cpp
b) also read about 'makefiles', something which helps tackling the
build process.
</OffTopic>
Observe that the errors you have quoted are all linker errors, hence
this is probably off-topic here.
-N
Thanks to all.
But why the file
//file fig003_13.cpp
#include "GradeBook.h"
change to
#include "GradeBook.cpp"
this way works fine.
Why not like this!?
thanks in advance.

Because by including the .cpp file you are including the source code which
gets compiled into one compilation unit (object) instead of 2.

But including a .cpp in a .cpp if a definate no no. You should put it back
the way it was and link the object files together.

Thanks to all and sorry for this off topic post (now realized!).

Bye.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top