C++ : 错误:类型“String*”的无效操作数和“const char [7]”到二元“运算符”

发布于 2024-08-02 10:04:51 字数 1069 浏览 4 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-08-09 10:04:51

您可能不想使用指向 String 类的指针。尝试以下代码:

int main(int argc, char* argv[]) {
    String testing = String("Hello, "); //works
    testing.print();//works
    String a = testing+"World!";
    return 0;
}

在为 C++ 类型定义新运算符时,您通常会直接使用实际类型,而不是指向您的类型的指针。像上面这样分配的 C++ 对象(如字符串测试)是在堆栈上分配的(生存到“作用域”或函数结束)而不是堆上(生存到程序结束)。

如果您确实想使用指向您的类型的指针,则可以像这样修改最后一行:

String *a = new String(*testing + "World!");

但是,按照 std::string 的示例,这不是您通常希望使用这样的字符串的方式班级。

You probably don't want to use a pointer to your String class. Try this code:

int main(int argc, char* argv[]) {
    String testing = String("Hello, "); //works
    testing.print();//works
    String a = testing+"World!";
    return 0;
}

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:

String *a = new String(*testing + "World!");

However, following the example of std::string this is not how you would normally want to use such a string class.

残月升风 2024-08-09 10:04:51

您的operator+是为Stringconst* char定义的,而不是为String*定义的。在添加测试之前,您应该取消引用测试,即:

String a = (*testing) + "World";

尽管在这种情况下,我不认为首先测试指针有什么意义。

编辑:创建一个没有指针的字符串将如下所示:

String testing = "Hello, ";

String testing("Hello, ");

(两者是等效的)。

Your operator+ is defined for String and const* char, not for String*. You should dereference testing before adding it, i.e.:

String a = (*testing) + "World";

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:

String testing = "Hello, ";

or

String testing("Hello, ");

(both are equivalent).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文