overloading classes in braces

Greetings! I'm working through C++ Primer Plus (Prata, 5E) and am working with operator overloading. The exercise I'm working on deals with converting char * to custom String class objects.

My trouble lies in writing overloading functions for this:
String rgb[3] = { String(s1), String("green"), String("blue")};

Assigning the individual arguments to String objects works, but bringing them together in the above assignment has me stumped.

While the following doesn't work, I thought it might help explain what I'm trying to do:

1
2
3
4
5
6
7
8
9
String * String::operator{}(const String s0, const String s1, const String s2)
{
	String * StrArr = new String[3];	// size is stuck at 3
	StrArr[0] = s0;
	StrArr[1] = s1;
	StrArr[2] = s2;

	return StrArr;
}


Any help or suggestions would be very much appreciated. Have an excellent day
You can't overload the {} operator. C++ doesn't allow it.

I don't understand why you're trying to, anyway. Your first line of code would work just fine without overloading the {} operator.
I'm getting Linker errors (LNK1120 and LNK2019 with Visual Studio C++ 2008) and thought attempting to overload the braces (which isn't allowed) might fix it. I'm combing over my code and searching those errors for clues.
Thanks!
If you're getting "unresolved external symbol" errors, that has nothing to do with the braces. You are way on the wrong track.

What that error usually means is either:

1) You prototyped a function but never gave it a body

or

2) You are using an library but forgot to link to the library.

If you can post the full error in here, I can help diagnose exactly what the problem is.
Linker refers to your .LIB files.

Download cryptopp source. Find it on google. Download it and build the cryptlib.vcproj in Debug and Release Modes.

Building process:

1. Download cryptopp.
2. Copy .h files from 'include' folder and paste it into 'surce' folder in cryptopp.
3. open cryptlib.vcproj file in VC++.
4. Build the solution in Debug and Release modes.
5. Rename Cryptlib.lib in debug folder to 'Cryptlib08.lib' and rename cryptlib.lib in release folder to 'cryptlib08d.lib' .
6.Copy both the renamed .LIB files into your project LIB folder or declare the address of your linker into your project. ( Its better you copy both files in your Linker folder).
I don't know what cryptopp is, but if you aren't using it for your project, you certainly don't need to download and install it. And there's no way it is causing your linker errors if you don't have it installed in the first place.

I would disregard Ronny's post for now and just show us your error message. I'd wager my lunch money that it's due to a prototyped function with no body.
Last edited on
Hey, thank you for the help. I looked over my class methods and there were several prototypes without definitions. When I commented the prototypes out, the link errors went away. Some new ones popped up upon runtime, but I think I just need to go back and understand the topics a little better.

Thanks a ton!
Topic archived. No new replies allowed.