C++ : 错误:类型“String*”的无效操作数和“const char [7]”到二元“运算符”
我正在学习 cpp,在我的上一份作业中,我正在重写 std::string 类。 所以这是我的代码的概述: string 类:
class String {
public:
String(const char* sInput) {
string = const_cast<char*> (sInput);
}
const String operator+(const char* str) {
//snip
print();
}
void print() {
cout<<string;
}
int search(char* str) {
}
private:
char* string;
int len;
};
哦,我不得不说我尝试将该方法声明为 String* operator+(const char* str) 和 const String&运算符+(const char* str) 没有变化。 这是我运行它的方式:
int main(int argc, char* argv[]) {
String* testing = new String("Hello, "); //works
testing->print();//works
/*String* a = */testing+"World!";//Error here.
return 0;
}
完整的错误如下所示:
foobar.cc:13: 错误:操作数无效 类型“String*”和“const char” [7]' 到二进制 'operator+'
我在 Google 上查找,在我正在学习的书中没有成功。 有人有建议吗? (我很确定我做了一些愚蠢的事情,你必须原谅我,我最初是一名 PHP 程序员)任何人都可以指出我错过了什么吗?
I'm learning cpp and In my last assignment I am rewriting the std::string class.
so here is an outline of my code:
string class:
class String {
public:
String(const char* sInput) {
string = const_cast<char*> (sInput);
}
const String operator+(const char* str) {
//snip
print();
}
void print() {
cout<<string;
}
int search(char* str) {
}
private:
char* string;
int len;
};
Oh and I have to say I tried to declare the method as String* operator+(const char* str) and as const String& operator+(const char* str) with no change.
And here is how I run it:
int main(int argc, char* argv[]) {
String* testing = new String("Hello, "); //works
testing->print();//works
/*String* a = */testing+"World!";//Error here.
return 0;
}
The full error goes like such:
foobar.cc:13: error: invalid operands
of types ‘String*’ and ‘const char
[7]’ to binary ‘operator+’
I looked up on Google and in the book I am learning from with no success.
any one with suggestions? (I am pretty sure I am doing something foolish you will have to forgive me I am originally a PHP programmer) can any one point me to what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能不想使用指向
String
类的指针。尝试以下代码:在为 C++ 类型定义新运算符时,您通常会直接使用实际类型,而不是指向您的类型的指针。像上面这样分配的 C++ 对象(如
字符串测试
)是在堆栈上分配的(生存到“作用域”或函数结束)而不是堆上(生存到程序结束)。如果您确实想使用指向您的类型的指针,则可以像这样修改最后一行:
但是,按照
std::string
的示例,这不是您通常希望使用这样的字符串的方式班级。You probably don't want to use a pointer to your
String
class. Try this code:When defining new operators for C++ types, you generally will work with the actual type directly, and not a pointer to your type. C++ objects allocated like the above (as
String testing
) are allocated on the stack (lives until the end of the "scope" or function) instead of the heap (lives until the end of your program).If you really want to use pointers to your type, you would modify the last line like this:
However, following the example of
std::string
this is not how you would normally want to use such a string class.您的operator+是为
String
和const* char
定义的,而不是为String*
定义的。在添加测试之前,您应该取消引用测试,即:尽管在这种情况下,我不认为首先测试指针有什么意义。
编辑:创建一个没有指针的字符串将如下所示:
或
(两者是等效的)。
Your operator+ is defined for
String
andconst* char
, not forString*
. You should dereferencetesting
before adding it, i.e.:Though in this case I don't see the point in making testing a pointer in the fist place.
Edit: Creating a string without pointers would look like this:
or
(both are equivalent).