0

I am working in c++ /ubuntu. I have:

libr.hpp

#ifndef LIBR
#define LIBR


#include <string>
using namespace std;

class name
{
    public:
    name();
    ~name();
    std::string my_name;
    std::string method (std::string s);
    
};


#endif

and

libr.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;


name::name()
{

}
std::string name::method(std::string s)
{
    return ("YOUR NAME IS: "+s);
}

From these two I've created a libr.a.

In test.cpp:

#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"

using namespace std;

int main()
{
    
    name *n = new name();
    n->my_name="jack";
    cout<<n->method(n->my_name)<<endl;
    return 0;
}

I compile with g++ and libr.a. I have an error: "name::name() undefined reference", why?

I would like to mention that I've added in qt creator at qmake the .a. When I compile, I have the error. How can I solve it?

Community
  • 1
  • 1
skywak
  • 137
  • 3
  • 4
  • 10
  • 1
    Side comment: Why are you using `using namespace std;`? It is now considered bad form, and in a header it is considered extremely bad form. You don't need it at all in your header as it explicitly uses `std::string`. – David Hammen Jul 18 '11 at 11:12
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – sashoalm Feb 06 '15 at 14:29

3 Answers3

1

This is a linker error, not a compiler error. It means that you have called but you have not defined the constructor. Your allocation name *n = new name(); calls the constructor.

Since you defined the constructor in your libr.cpp, what this means is that this compilation unit is not making its way into your executable. You mentioned that you are compiling with libr.a. When you compile your libr.cpp the result is a .o file, not a .a file.

You are not linking libr.o into your executable.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • 1
    @skywak: I do understand that English might not be your native language, but "yes i do" does not parse. Yes, you do **what**? Could you rewrite that last question? Also, if you would edit your question to show the steps taken to build your project we would be able to pinpoint your error. One last point: Start accepting answers. You have made nine questions and haven't accepted one answer yet. – David Hammen Jul 18 '11 at 13:16
0

What are the steps you're using to compile your "project"?

I performed the following steps and managed to build it with warnings/errors.

g++ -Wall -c libr.cpp

ar -cvq libr.a libr.o

g++ -Wall -o libr main.cpp libr.a

One last thing, if I change the order off the last command, like

g++ -Wall -o libr libr.a main.cpp

I get the following error:

Undefined                       first referenced
 symbol                             in file
name::name()                        /tmp/cc4Ro1ZM.o
name::method(std::basic_string<char, std::char_traits<char>, std::allocator<char
> >)/tmp/cc4Ro1ZM.o
ld: fatal: Symbol referencing errors. No output written to libr
collect2: ld returned 1 exit status
bruno
  • 2,802
  • 1
  • 23
  • 23
0

in fact , you needn't define the destructor yourself because the default destructor will be used when the class calling is over. and in the VS2008,it's all right!