code fixing

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

code fixing

 
0
  #1
Nov 12th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: code fixing

 
0
  #2
Nov 12th, 2008
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?
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: code fixing

 
0
  #3
Nov 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: code fixing

 
0
  #4
Nov 12th, 2008
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" )
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: code fixing

 
0
  #5
Nov 12th, 2008
yeah I noticed the dot operator from another fix I did, that was pretty simple. Now I'm going to look up constructors.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: code fixing

 
0
  #6
Nov 12th, 2008
Okay, I made ThreeVals not a void function and it seems to have worked. Tell me if this is correct or not please.

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: code fixing

 
0
  #7
Nov 12th, 2008
yep, you got it.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 378 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC