no suitable conversion function from "std::string" to "std::string*" exists

Hey all,

I'm currently taking a c++ course, I've tried asking my prof, but have only gotten unhelpful responses. Hoping for some insight to my issue or possibly getting pointed in the right direction.

I am attempting to call a function (toString()) from an initial function (getShape()) which is supposed to return a string (newShape->toString()), then assigning a string pointer (shape_ptr) to the returned string (newShape->toString()) and then returning the string pointer (shape_ptr) from the initial function (getShape()).

My question is how do I assign a string pointer to a string?

This is only part of the code, if more info is needed I can post more. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
string* shape_ptr //defined shape_ptr earlier in the initial function
Circle *newShape = new Circle(color, radius); //Creates new shape
shape_ptr = newShape->toString(); //This is the part that does not work, I need to 
//find a way to assign this string pointer shape_ptr to the returned string of toString().
return shape_ptr;

//returns color, type, measurements and area 
string Circle::toString() { 
return getColor() + ", " + shapeType + ", " + to_string(radius) + ", " + to_string(area());
};
Last edited on
Can we see the exact assignment text? Because what you're doing seems awfully unnecessary.

Why do you need to deal with std::string pointers?
Why not just have a string instead of a string pointer?

Anyway, here's how you could use a string pointer, if that's really necessary for your assignment. Not sure if it's what the professor wants.

1
2
3
4
5
6
string my_str;
string* ptr_str = &my_str;

// ...

*ptr_str = newShape->toString();
Last edited on
Sure thing. Most of the requirements I've already met with this function. It's the necessary return string pointer that I'm confused about. I think it's necessary since I need to later on sort the returned string data in main.

getShape.cpp

For reading shape data, create a getShape.cpp file containing a getShape() function that returns a base Shape * pointer. It should read a shape description from an input stream, create the correct type of derived shape with the new operator and parameters to the constructor, and return a base Shape * pointer to the new object. After reading a shape color and type (e.g. blue circle), it reads the additional information specific to that type of shape (e.g. for a circle, it reads the radius), and then uses the new operator to create the specific derived type of shape (e.g. new Circle(color, radius) )

Input can be from cin, or from a stream input file (use iostreams). When there is no more input data (or when 'done' is entered), return a NULL Shape pointer. All character data is kept in C++ strings (no char[] arrays).

This is the only file that #includes the Circle.h, Square.h, and Rectangle.h header files (since it needs them to create the various types of shapes with the new operator).


I'm also noticing she asks "Once all the shapes have been read, main() then loops, printing the list of shapes by calling the toString() member function on each Shape* pointer in the array." I'm thinking I might be going about this the wrong way and should just return "newShape" in getShape() and then deal with the toString() function later.
That assignment text does not tell you to use string pointers. You are using pointers for the Circle/Square/Shape polymorphism, but not for the strings themselves.
Last edited on
Yeah, I'm seeing that it's an issue of I didn't read/understand the directions. I changed the return type to Shape* and returned newShape to main() and I'm getting the expected results. Thank you!
Topic archived. No new replies allowed.