-3

I have a header file where I have defined a class and also used a unique_ptr to access the class members.
But I don't know how to access it from clone.cpp Generally, what we do is create an object of the class For example:

A obj;
bool res = obj.concate("hello");

How can we do it using unique_ptr?

When I am trying to do

bool result = access->concate("hello");

I am getting the following error:

Undefined symbols for architecture x86_64:
 "obja()", referenced from:
     _main in classA.o
ld: symbol(s) not found for architecture x86_64
clone.h
--------

std::unique_ptr<class A> obja();

class A
{
public:
  bool concate(string str){
  string a = "hello";
  return a == str;
  }
private:
   string b = "world";
};


clone.cpp
________

int main(){
 auto access = obja();
 return 0;
}
user0042
  • 7,917
  • 3
  • 24
  • 39
pavikirthi
  • 1,569
  • 3
  • 17
  • 26
  • 2
    Same way as you dereference raw pointers, using the `->` operator. – user0042 Oct 06 '17 at 16:46
  • Oh boy: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – user0042 Oct 06 '17 at 17:01
  • The error seems pretty clear. You have declared a function named `obja`, and you are calling it - but you haven't actually implemented it. – Igor Tandetnik Oct 06 '17 at 17:24

2 Answers2

0

You don't need class in std::unique_ptr<class A> obja();, same about brackets.

Not sure what r you trying to achieve here: auto access = obja(); I assume you tried to call concate there, then everything can be like this:

class A
{
public:
   bool concate(string str) {
      string a = "hello";
      return a == str;
   }
private:
   string b = "world";
};

std::unique_ptr<A> obja;

int main() {
   auto access = obja->concate("test");
   return 0;
}
StahlRat
  • 1,199
  • 13
  • 25
0
    class A
 { 
       public:
         bool concate(string str) { string a = "hello"; return a == str; } 

       private: string b = "world"; 
};

std::unique_ptr<A> obja = std::make_unique <A>; 

int main() { 
   auto access = obja->concate("test"); return 0;
 }

You just forget allocate memory. :)

CMLDMR
  • 334
  • 2
  • 12