views:

1397

answers:

2

Hi, 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?

+3  A: 

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.

Greg Hewgill
Good idea, but now when i do this:String testing = new String("Hello, ");I get the following(same) error:foobar.cc:11: error: conversion from ‘String*’ to non-scalar type ‘String’ requested
Leon
Notice I removed the keyword "new" from that line in my example. Using "new" allocates something on the heap and returns a pointer to it; without "new" it's a constructor call for an object on the stack.
Greg Hewgill
Oh right, thanks!
Leon
You should use `String testing("Hello, ");` instead of `String testing = String("Hello, ");`, which will needlessly create a temporary String object and call the copy constructor.
sepp2k
@sepp2k: In the case of assignment of a single value in the context of variable declaration (`T x = T(y)`), the compiler is permitted to consider that the same as `T x(y)` without the assignment.
Greg Hewgill
Well, at least gcc does not compile `T x = T(y);` if T's copy constructor is private.
sepp2k
There's a requirement in the standard that even if the compiler performs the optimisation, it must still ensure that the copy constructor is accessible, and if it is not then the program is invalid and must be rejected. One of the rare instances where the standard goes out of its way to stop you doing something that "works on my machine" :-)
Steve Jessop
+2  A: 

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).

sepp2k
That worked :) , But now I am trying not to use the pointer notation upon construction and i get the same error on the construction line:foobar.cc:11: error: conversion from ‘String*’ to non-scalar type ‘String’ requestedI have to admit that these pointers is quite confusing to the begginer.
Leon