Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › help with c++ headers....
New Posts  All Forums:Forum Nav:

help with c++ headers....

post #1 of 12
Thread Starter 
so for my programming class we were given this problem: (on codelab)

Write the interface (.h file) of a class Player containing:
A data member name of type string .
A data member score of type int .
A member function called setName that accepts a parameter and assigns it to name . The function returns no value.
A member function called setScore that accepts a parameter and assigns it to score . The function returns no value.
A member function called getName that accepts no parameters and returns the value of name .
A member function called getScore that accepts no parameters and returns the value of score .

and I came up with this code, but it tells me I am wrong (and won't show me the compiler errors...)
Code:
class Player
{
private:
string name;
int score;
public:
void setScore(int doobry) { score=doobry; }
void setName(int foobar) { name=foobar; }
string getName() { return name; }
int getScore() { return score; }
};


can anyone see what im doing wrong?
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
post #2 of 12
Thread Starter 
bump
i tried this a million different ways, nothing seems to work. i know its not that hard, but i feel figuring this out will make the rest of the assignment a lottt clearer
I WILL REP for any help. im not asking for a straight answer!!!!!!!!!!!

this is the .h file

what would be different in the .cpp file?
Edited by ccrunner863 - 4/12/11 at 9:01pm
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
post #3 of 12
Thread Starter 
bump....anyone????
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
post #4 of 12
I'm used to C# buddy, can't help you on this one, it doesn't seem hard but I just don't get the syntax of c++.
post #5 of 12
Have a look at this. The tut is old but shows how it should be done.
http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
Ever Changing
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 GO 400x9 1.392v p5q pro turbo MSI Twin Frozr III 1G/OC 6950 Kingston HX (4x1gb 800mhz) 
Hard DriveOptical DriveOSMonitor
WD 80gb 16mb cache DVd rw win 7 24" ctl 
PowerCase
Rx 630SS CM 690 II Advanced 
  hide details  
Reply
post #6 of 12
did you write the include directives?

you will get errors without including the directives.
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #7 of 12
Just casually glanced at it but your trying to set a string value name equal to an integer value foobar..
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #8 of 12
Code:
void setName(int foobar) { name=foobar; }

You need that fucntions input parameters to be a string, not a int.

Other than that it looks good. You just need to #include "filename.h" in whatever .cpp file you are using it with and everything should work just fine.
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #9 of 12
Quote:
Originally Posted by ccrunner863;13105767 
so for my programming class we were given this problem: (on codelab)

Write the interface (.h file) of a class Player containing:
A data member name of type string .
A data member score of type int .
A member function called setName that accepts a parameter and assigns it to name . The function returns no value.
A member function called setScore that accepts a parameter and assigns it to score . The function returns no value.
A member function called getName that accepts no parameters and returns the value of name .
A member function called getScore that accepts no parameters and returns the value of score .

and I came up with this code, but it tells me I am wrong (and won't show me the compiler errors...)
Code:

[B]// player.h
#include <iostream>
#include <string>
using namespace std;[/B]


class Player
{
private:
string name;
int score;

public:
void setScore(int doobry) { score=doobry; }
[B]void setName(string foobar) { name=foobar; }[/B]
         string getName() { return name; }
int getScore() { return score; }
};


//player.cpp
//do whatever here

can anyone see what im doing wrong?

changes in bold...you have to use the include string directive...in order to use strings...also change the setName parameter to a string not an int...then it should be golden.
Edited by surfbumb - 4/12/11 at 9:26pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #10 of 12
Thread Starter 
thanks guys i changed setName to a string and it worked!

its on codelab so i didnt need the #include at the top of the file
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
Back in the game
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 ASUS P7P55D Deluxe SLI MSI GTX 460 1GB factory OC 8GB Patriot Signature 1600 
Hard DriveOSMonitorPower
500 GB 7200.12 and 60gb Vertex 3 SSD 7 Ultimate LG Flatron w2343 Seasonic 750 Gold 
Case
NZXT M59 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › help with c++ headers....