g++ problem!!!

M

milicic.marko

I have very simple problem to solve.

I have three files

--
first is: main.cpp
--
#include "osoba.h"

int main (void)
{
osoba o;
o.stampaj();
}



--
Second is "osoba.cpp"
--
#include "osoba.h"

void
osoba::stampaj()
{
std::cout<<"zdravo";
}

osoba::eek:soba()
{
}

osoba::~osoba()
{
}

--
Third is: "osoba.h"
--
#ifndef OSOBA_H
#define OSOBA_H

#include <string>

class osoba
{

public:

osoba();
~osoba();
void stampaj();

private:

std::string _ime;
std::string _nadimak;

};
#endif


When I complile, I got this error:
bash-2.05b$ g++ -o main main.cpp
/tmp/ccLgxIrl.o(.text+0x19): In function `main':
: undefined reference to `osoba::eek:soba[in-charge]()'
/tmp/ccLgxIrl.o(.text+0x28): In function `main':
: undefined reference to `osoba::stampaj()'
/tmp/ccLgxIrl.o(.text+0x3f): In function `main':
: undefined reference to `osoba::~osoba [in-charge]()'
/tmp/ccLgxIrl.o(.text+0x5c): In function `main':
: undefined reference to `osoba::~osoba [in-charge]()'
collect2: ld returned 1 exit status
bash-2.05b$


How should I solve this problem?
 
J

John Smith

When I complile, I got this error:
bash-2.05b$ g++ -o main main.cpp

You have main.cpp and osoba.cpp but you only compiled main.cpp. Try

g++ -o main main.cpp osoba.cpp

-- John
 
L

Larry Brasfield

I have very simple problem to solve. [Code cut as redundant.]
When I complile, I got this error:
bash-2.05b$ g++ -o main main.cpp
/tmp/ccLgxIrl.o(.text+0x19): In function `main':
: undefined reference to `osoba::eek:soba[in-charge]()'
/tmp/ccLgxIrl.o(.text+0x28): In function `main':
: undefined reference to `osoba::stampaj()'
/tmp/ccLgxIrl.o(.text+0x3f): In function `main':
: undefined reference to `osoba::~osoba [in-charge]()'
/tmp/ccLgxIrl.o(.text+0x5c): In function `main':
: undefined reference to `osoba::~osoba [in-charge]()'
collect2: ld returned 1 exit status
bash-2.05b$

I cannot see why, with the proper invocation, and
the required include of <iostream> (added below),
your code should not compile. Your invocation
did not include compilation and linking of osoba.cpp,
so the complaints you got are not surprising since
you (implicitly) asked for an executable image.

Here can be seen a CLI shell session:
=========== screen scrape begins ============
[C:\tmp]
g++ osoba.cpp main.cpp
[C:\tmp]
type osoba.cpp
#include "osoba.h"

void
osoba::stampaj()
{
std::cout<<"zdravo";
}

osoba::eek:soba()
{
}

osoba::~osoba()
{
}


[C:\tmp]
type osoba.h
#ifndef OSOBA_H
#define OSOBA_H

#include <iostream>
#include <string>

class osoba
{

public:

osoba();
~osoba();
void stampaj();

private:

std::string _ime;
std::string _nadimak;

};
#endif

[C:\tmp]
type main.cpp
#include "osoba.h"

int main (void)
{
osoba o;
o.stampaj();
}

[C:\tmp]
a.exe zdravo
[C:\tmp]
g++ --version
g++ (GCC) 3.3.3 (cygwin special)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[C:\tmp]
=========== screen scrape ends ============
 
M

milicic.marko

Thanks you all.

It was all about compiling one instead of two files.

Silly me!

Ciao
 

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
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top