943,850 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 537
  • C++ RSS
Nov 12th, 2008
0

code fixing

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct ThreeVals
  5. {
  6. int a, b, c;
  7. void ThreeVals()
  8. {a = 1; b = 2; c = 3;}
  9. };
  10. int main()
  11. {
  12. ThreeVals vals;
  13. cout << vals << endl;
  14. return 0;
  15. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 12th, 2008
0

Re: code fixing

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?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 12th, 2008
0

Re: code fixing

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 12th, 2008
0

Re: code fixing

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" )
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 12th, 2008
0

Re: code fixing

yeah I noticed the dot operator from another fix I did, that was pretty simple. Now I'm going to look up constructors.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 12th, 2008
0

Re: code fixing

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct ThreeVals
  5. {
  6. int a, b, c;
  7.  
  8. ThreeVals()
  9. { a = 1;
  10. b = 2;
  11. c = 3;
  12. }
  13. };
  14. int main()
  15. {
  16. ThreeVals vals;
  17. cout << vals.a << vals.b << vals.c << endl;
  18. return 0;
  19. }
Last edited by Foe89; Nov 12th, 2008 at 3:23 pm. Reason: code mistype
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 12th, 2008
0

Re: code fixing

yep, you got it.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with classes and arrays
Next Thread in C++ Forum Timeline: Checking Whether a folder is a network share





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC