I don't usually ask for help on these but I missed a day of class and I'm a bit out of the loop. We're going over structures and for HW my instructer likes to do code fixes where theres code with errors that we need to correct. Anyways, I'm reading through my book and I'm not finding any examples that are similar to this problem so I'm not sure what exactly I need to do to correct it. Theres nothing so far that has a void function inside of a structure, with the main called the function.

#include <iostream>
using namespace std;

struct ThreeVals
{
	int a, b, c;
	void ThreeVals()
	{a = 1; b = 2; c = 3;}
};
int main()
{
	ThreeVals vals;
	cout << vals << endl;
	return 0;
}

Well, you could start by reading the compiler error messages, they will at least point you to (or near) the places that need fixing.

Read your textbook for the examples of how structures/classes should look - what's the difference compared to your problem?

errors are:

error C2380: type(s) preceding 'ThreeVals' (constructor with return type, or illegal redefinition of current class-name?)

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'struct ThreeVals' (or there is no acceptable conversion)

And I already said that I looked in my text book. I can't find anything in the section that has a void function in the structure. I don't know if there's special rules or what not. I just see normal structures that declare ints doubles, and strings.

And I don't know what the errors mean. I'm looking through my book, but like I said there's no examples that are similar to this. I've already fixed one set of code and am working on another. This one just bugs me.

The first one is not something we do in something that is strictly a struct. If your text has another section dealing with classes, look at the explanation of a constructor, and how it's implemented. It's a function, but not written exactly like the functions you've written up to this point.

The second one means that there's no way (at the moment) to directly output the contents of your struct. You have to display it one member at a time. This, I'm sure, is covered in your text. (Look for the "dot operator" )

yeah I noticed the dot operator from another fix I did, that was pretty simple. Now I'm going to look up constructors.

Okay, I made ThreeVals not a void function and it seems to have worked. Tell me if this is correct or not please.

#include <iostream>
using namespace std;

struct ThreeVals
{
	int a, b, c;

	ThreeVals()
{	a = 1; 
	b = 2; 
	c = 3;
}
};
int main()
{
	ThreeVals vals;
	cout << vals.a << vals.b << vals.c << endl;
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.