简体   繁体   中英

why i am getting error " (name of the pointer ) is not a template )?

I wrote this code just for practice to test the concept of copy constructor and destructor, but it is showing me an error that shared pointer is not a template and my most of the pointer is undefined as per compiler.

I want to know whats wrong.

class MyString
 {
   public:
       MyString(); // default ctor
       MyString(string const &); // ctor with ordinary string argument
       MyString(MyString const &); // copy ctor
       MyString(MyString &&); // move ctor – discussed later
      ~MyString(); // destructor
       MyString& operator=(MyString const &rhsObject); // assignment operator
       void showString(); // outputs the String in the object

 private:
     shared_ptr<string> rep;
}; // end class MyString

MyString::MyString()
{
    // creates an object with empty string
    rep = make_shared<string>();
}


MyString::MyString(string str)
{
    // creates an object with an ordinary string pointed to
    rep = make_shared<string>(str);
}


MyString::MyString(MyString const &ob)
{
    // creates an object with String as in ob
rep = make_shared<string>(ob.rep);
}
MyString::~MyString()
{
}


MyString & MyString::operator=(const MyString &ob)
{
    // resets the value of calling object to the value of ob
    rep = make_shared<string>(ob.rep);
    return *this; // “this” pointer explained below
}


void MyString::showString()
{
    // outputs the string pointed to by rep
    cout << *rep;
} 

void main()
{
    MyString a("Hello"), b = "CS 570", c("Students");
    // constructor MyString(char *) creates objects a, b, and c
    MyString d; // default constructor MyString() creates d
    MyString e(a), f = c;
    // copy constructor MyString(MyString &) creates objects e and f
    cout << "String in object a = ";
    a.showString();
    cout << endl; cout << "String in object b = "; b.showString(); cout << endl;
    cout << "String in object c = ";
    c.showString(); cout << endl;
    cout << "String in object d = ";
    d.showString(); cout << endl;
    cout << "String in object e = ";
    e.showString(); cout << endl;
    cout << "String in object f = ";
    f.showString(); cout << endl;
    d = c;
    cout << "Object d reset to have String of c : "; d.showString(); cout << endl;
    c = a;
    cout << "Object c reset to have String of a : "; c.showString();
    cout << endl;
    //Copy constructor and this pointer 7
    cout << "Object d’s string remains as before : "; d.showString();
    cout << endl;
} // end main function
#include <string>
#include <memory>
#include <iostream>

class MyString
{
  public:
    MyString(); 
    MyString(std::string str1); 
    MyString(MyString const &); 
    MyString(MyString &&); 
    ~MyString(); 
    MyString& operator=(MyString const &rhsObject); 
    std::string showString(); 

private:
    static std::shared_ptr<std::string> rep;
}; 


MyString::MyString()
{
  rep = std::make_shared<std::string>();
}


MyString::MyString(std::string str1)
{
  rep = std::make_shared<std::string>(str1);
}


MyString::MyString(MyString const &ob)
{
  rep = std::make_shared<std::string>(ob.rep);
}

MyString::~MyString()
{

}


MyString & MyString::operator=(const MyString &ob)
{
  rep = std::make_shared<std::string>(ob.rep);
  return *this; 
}


std::string MyString::showString()
{
   return *rep; 
}

int main()
{ 

    MyString a("Hello"),
             b("CS 570"),
             c("Students");

    MyString d; 
    MyString e(a), f = c;
    std::cout << a.showString() << std::endl;
    std::cout << "\n" << a.showString() << std::endl;
    std::cout << "String in object b = " << b.showString() << std::endl;
    std::cout << "String in object c = ";
    std::cout << c.showString() << std::endl;
    std::cout << "String in object d = ";
    std::cout << d.showString() << std::endl;
    std::cout << "String in object e = ";
    std::cout << e.showString() << std::endl;
    std::cout << "String in object f = ";
    std::cout << f.showString() << std::endl;
    d = c;
    std::cout << "Object d reset to have String of c : " << d.showString() << std::endl;
    c = a;
    std::cout << "Object c reset to have String of a : " << c.showString() << std::endl;
    std::cout << "Object d’s string remains as before : " << d.showString() << std::endl;

return 0;

}

Alright this is an update on my previous example, I re-wrote all of your code and knocked out the 100+ errors down to 1 error, I left this error intentionally because this is some thing you will encounter and have to learn to get around on your own.

I would suggest even though this is small, breaking this up into a your main source, header for your class, and source for your functions to make it easier for you to debug your stuff.

As for the remaining issues, Why are you using all of these constructors????

You can do what ever you're trying with 1 constructor (2, or even 3) and the rest can be functions.

You asked what was wrong, and the answer is most of your code all I did was re-write and fix minimal issues that will allow you to progress and learn to fix your own code.

  1. Don't just use constructors, do more research on const variables and reference parameters, read up on pointers I would really suggest re-writing all of this as the way you've got it setup now will conflict with memory header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM