Sign in to follow this  
Orrill

using struct

Recommended Posts

I am trying to complete the following exercise:
Quote:
Create a struct that holds two string objects and one int. Use a typedef for the struct name. Create an instance of the struct, initialize all three values in your instance, and print them out.
This is my attempt off the top of my head...
#include <iostream>
#include <cstring>
using namespace std;

struct myStruct
{
	string obj1;
	string obj2;

	int int1;
};


int main()
{
	myStruct myInstance;

	myInstance.obj1 = "hello";
	myInstance.obj2 = "I am ";
	myInstance.int1 = 25;

	cout << myInstance.obj1;
	cout << myInstance.obj2;
	cout << myInstance.int1;

	cin.get();

	return 0;
}


...which doesn't work :( Could someone explain why I get the error message:
Quote:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Thanks.

Share this post


Link to post
Share on other sites
Is this homework of some sort, or just an exercise you're doing on your own?

I think it would be helpful (to yourself and to us) to clarify whether the program is supposed to be written in C++, or C. The mention of a typedef'ed struct suggests the latter, while the mention of 'string objects' suggests the former.

As for your current code, here's a hint: double-check to make sure you're including the correct header files.

Share this post


Link to post
Share on other sites
These exercises that I am doing on my own, the program is supposed to be wrote in c++, the typedef is there because the book I am studying also includes references for/from the C language, so I just ignored the typedef part.

Share this post


Link to post
Share on other sites
Quote:
Original post by Orrill
These exercises that I am doing on my own, the program is supposed to be wrote in c++, the typedef is there because the book I am studying also includes references for/from the C language, so I just ignored the typedef part.
Got it. Yeah, then just double-check your header files.

Share this post


Link to post
Share on other sites
string instead of cstring!!!

Well that was about 15mins of headache for the simplest of thing.

I'm guessing this is because the C language didn't have a string.h header file which means there was no conversion and so there is no cstring header file.

Ultimately meaning that the string class was intoduced with c++


Thanks for the help jyk.

Share this post


Link to post
Share on other sites
Quite the opposite, actually. C++ may not have string.h, but C does. the string.h file has utilities for mucking around with so-called C-strings, things like this:
#include <string.h>
int main()
{
char name[] = "Tom";
int = strlen(name); /* stores the number of letters in the char array name */
return 0;
}


There are also functions for concatenation and things like that. Things that std::string does automatically.

When c++ came around, the standard called for namespaces. All standard functionality was to be placed in the std namespace, and this included most of the C library. The standard also wanted to differentiate the library from the C one, so they removed the .h and added the c at the beginning of the header name.

Speaking of std::string, the standard decided to name the header for that string.

Hope that helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this