PDA

View Full Version : weird error



mickey
10th November 2006, 16:52
Hi, I've got this strange error that's only sometimes when I build app; why only sometimes? thanks...


linking ../bin/person (g++)
main.o: In function `main':main.cpp:(.text+0xf2): undefined reference to `Person::setName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make[1]: *** [../bin/person] Error 1
make[1]: Target `first' not remade because of errors.
make: *** [sub-src] Error 2
make: Target `first' not remade because of errors.
*** Exited with status: 2 ***




class Person{
private:
string name;
....................
};
inline void Person::setName(const string n) {
name = n;
}
p.setName("we we we\n");

mickey
10th November 2006, 21:06
hi,
but I cut out keyword 'inline' and it work...but why? Isn't possibile to use 'inline' out a class?? (could be possible microsoft compiler permit it?)

mickey
10th November 2006, 22:08
HI guys,
I read in a book that 'inline function' should be defined in evey file where it's used'; so it's better define it after class declaration:


//person.h
class Person{
private:
string name;
public:
Person();
~Person();
string getName() const {return name;}
inline void setName (const string n);
};

inline void Person::setName(const string n) {
this->name = n;
}

in this way it work! But I remember that my prevoius code work with .net; could anyone confirm that? thanks

jacek
11th November 2006, 00:18
I read in a book
Excellent, keep it up. :)


that 'inline function' should be defined in evey file where it's used'; so it's better define it after class declaration: [...] in this way it work!
Yes, if you want to use inline function/method in more than one place, you have to put its implementation in a header file.

fritz
16th November 2006, 09:50
moreover, when you define a function body in the class declaration as you do with getName(), it automatically becomes inline. so you don't need to use inline keyword, just declare the class as



class Person{
private:
string name;

public:
Person();
~Person();

string getName() const {return name;}
void setName (const string n) { this->name = n; }
};

mickey
18th November 2006, 03:24
yes I knew that! but then: when must I do use inline keyword if the only use it's mine (but I could define it inside class)?

fritz
18th November 2006, 05:22
when you declare non-member inline functions, you must use inline keyword.