using struct

Started by
5 comments, last by nobodynews 16 years, 11 months ago
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.
Advertisement
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.
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.
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.
#include <cstring>             // Old C-style string stuff#include <string>              // C++ std::string

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

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement