[英]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.好的,这是对我之前示例的更新,我重新编写了您的所有代码并将 100 多个错误减少到 1 个错误,我故意留下这个错误,因为这是您会遇到的一些事情并且必须学会绕过靠你自己。
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.你可以用 1 个构造函数(2 个,甚至 3 个)做任何你想尝试的事情,其余的可以是函数。
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.你问出了什么问题,答案是你的大部分代码我所做的只是重写和修复最小的问题,让你进步并学习修复你自己的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.