Raised This Month: $ Target: $400
 0% 

C++ help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 08-29-2007 , 18:53   C++ help
Reply With Quote #1

can any one here help me with C++ , i have a project i need to finish , but im getting some errors

here are my erros

Code:
 
Computer.cpp(155): error C2040: 'CComputer::FindDriver' : 'int (CHardDrive *)' differs in levels of indirection from 'CDriver *(CHardDrive *)'
 
Computer.cpp(155): error C2556: 'int CComputer::FindDriver(CHardDrive *)' : overloaded function differs only by return type from 'CDriver *CComputer::FindDriver(CHardDrive *)'


computer.cpp

Code:
 
/* 
Part 2 :
Identify What The Prgoram Does
this program is "getting" hard ware and software info , drivers and hard drives , and reporting when the power for the
pc is turned on ,the drivers are laoding , and also reports the free speace, the amount of space the hard drives have , 
and space used. then turns the pc off.
*/
 
 
#include "stdafx.h"
#include "Computer.h"
int total;
CComponent::CComponent(char *name ) { // doesnt need to be const
m_name = (char *) malloc(strlen(name)); // got "cant convert const char to char" error , so i changed them all to char
strcpy(m_name, name);
m_status = false;
}
bool CComponent::PowerOn() { // paramters cuasing errors
if ( m_status )
returnfalse;
//if ( !driver->IsFor(this) ) //causing errors and doesnt really do anything , if status isnt true it will return false
//return false;
else// make the function work properly
m_status = true;
returntrue;
}
bool CComponent::PowerOff( ) {
if ( !m_status )
returnfalse;
m_status = false;
returntrue;
}
char CComponent::GetName() { // was a const char causing error becuase its a char in the header file
return *m_name; // needed * or there was a couldnt convert errror
}
CDriver::CDriver( char *comp_name ) { //no need to be const 
m_comp = (char *) malloc(strlen(comp_name));
strcpy(m_comp, comp_name);
}
int CDriver::IsFor( CComponent *comp ) {
return ( strcmp( comp->m_name, m_comp )); // was assigining the returned value to 0
}
CHardDrive::CHardDrive(char *name, int size) {
m_size = size;
}
 
bool CHardDrive::PowerOn( CDriver *driver) {
 
if ( !CComponent::PowerOn() ) // had the paramters from the originol bool here causing error
returnfalse;
printf("%s (%iGB) spinning up...\n", m_name, m_size); // used %d when size is an integer
returntrue;
}
int CHardDrive::GetSize( ) {
return m_size;
}
CComputer::CComputer( ) {
m_numDrivers = 0;
m_numHardDrives = 0;
m_status = false;
}
bool CComputer::PowerOn( ) {
if ( m_status )
returnfalse;
printf("Turning the computer on...\n");
// int total declared this globally so it can be used by my function also 
 
for (int i = 0; i < m_numHardDrives; i++) {
CHardDrive *hd = m_hardDrives[i];
CDriver *driver = FindDriver(hd);
if ( !hd->PowerOn(driver) )
returnfalse;
total += hd->GetSize();
}
printf("%iGB total hard drive space.\n", total); // used %d integer %i
m_status = true;
returntrue;
}
bool CComputer::PowerOff( ) {
if ( !m_status )
returnfalse;
printf("Turning the computer off...\n");
for (int i = 0; i < m_numHardDrives; i++)
m_hardDrives[i]; // had this marked as a class with power off , didnt make any sence
m_status = false;
returntrue;
}
bool CComputer::IsOn( ) {
return m_status;
}
bool CComputer::NewHardDrive(char *name, int size ) { // no need to be const
m_hardDrives[m_numHardDrives] = new CHardDrive(name, size);
m_numHardDrives++;
printf("%s (%iGB) hard drive installed.\n", name, size); // used %d for an integer again
returntrue;
}
bool CComputer::NewDriver(char *name ) { // no need to be const
m_drivers[m_numDrivers] = new CDriver(name);
m_numDrivers++;
printf("%s driver installed.\n", name);
returntrue;
}
CComputer::FindDriver(CHardDrive *hd ) {
for (int i = 0; i < m_numDrivers; i++) {
//if (m_drivers[i].IsFor(hd)) doesnt really do ne thing function will still return m_drivers or 0
return m_drivers[i];
}
return NULL;
}
// "uses " mem , and reports the free space by subtracting from the total mem declared glaobaly and used earlier
void WriteData()
{
int Used_Mem = 1;
printf("Functions Using 1GB Of Space.\n");
printf("Free Space Is %iGB",total - Used_Mem);
}
computer.h

Code:
#define MAX_DRIVERS 10
#define MAX_HARDDRIVES 10
class CComponent {
public:
CComponent(); // needed a defualt constructor else you get an error in harddrive function
CComponent(char *name); // used const whihc cuaesed errors in the cpomuter.cpp
char GetName( );
bool PowerOn(); // bool had paramters in the parenthesis casuging syntex errors
bool PowerOff();
/*
private:
was gonna use friend , but both of these need to be public 
*/
bool m_status;
char *m_name; // doesnt need to be const 
};
 
class CDriver {
public:
CDriver(char *comp_name ); // doeswnt need to be const
int IsFor( CComponent *comp ); // function is retunrnng numbers , needs to be int
private:
char *m_comp;
};
class CHardDrive : public CComponent{ 
public:
CHardDrive(char *name, int size );
bool PowerOn( CDriver *driver );
int GetSize( );
void WriteData();

private:
int m_size;
};
class CComputer {
public:
CComputer( );
bool PowerOn( );
bool PowerOff( );
bool IsOn( );
bool NewHardDrive( char *name , int size); // doesnt need to be const and was missing int paramater
bool NewDriver( char *name ); // doesnt need to be const
private:
CDriver *FindDriver(CHardDrive *hd ); // missing the * whihc is in the pararmters in the cpp.
bool m_status;
CDriver *m_drivers[MAX_DRIVERS]; 
int m_numDrivers;
CHardDrive *m_hardDrives[MAX_HARDDRIVES];
int m_numHardDrives;
};
Example.cpp

Code:
 
#include "stdafx.h"
#include "Computer.h"
int main() {
CComputer *desktop = new CComputer();
desktop->NewHardDrive("Western Digital",250); 
desktop->NewHardDrive("Western Digital", 320); 
desktop->NewHardDrive("Seagate",160); 
desktop->NewDriver("Western Digital");
desktop->NewDriver("Seagate");
desktop->PowerOn();
if ( desktop->IsOn() )
printf("Computer is on!\n");
else
printf("Computer is off.\n");
if ( desktop->IsOn() )
desktop->PowerOff();
system("pause");
}
stdafx.h

Code:
 
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#include <iostream>
#include <tchar.h>

Ive been lookin at the code for 8 -10 hours now and am only loosing my mind lol . any help would be greatly apreciated
Thx

Last edited by Peoples Army; 08-29-2007 at 18:55.
Peoples Army is offline
dalto
Veteran Member
Join Date: Jul 2007
Old 08-29-2007 , 19:10   Re: C++ help
Reply With Quote #2

Should be:
Code:
CDriver * CComputer::FindDriver(CHardDrive *hd)
On a side note. This:
Code:
for (int i = 0; i < m_numDrivers; i++) {
//if (m_drivers[i].IsFor(hd)) doesnt really do ne thing function will still return m_drivers or 0
return m_drivers[i];
}
is equivalent to:

Code:
return m_drivers[0];
which is probably not what you want.
dalto is offline
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 08-29-2007 , 19:15   Re: C++ help
Reply With Quote #3

um now i get thses errors

Code:
 
Example error LNK2019: unresolved external symbol "public: __thiscall CComponent::CComponent(void)" (??0CComponent@@QAE@XZ) referenced in function "public: __thiscall CHardDrive::CHardDrive(char *,int)" (??0CHardDrive@@QAE@PADH@Z)
 
 

Example fatal error LNK1120: 1 unresolved externals

but my other errors went away

Last edited by Peoples Army; 08-29-2007 at 19:20.
Peoples Army is offline
pRED*
Join Date: Dec 2006
Old 08-29-2007 , 19:58   Re: C++ help
Reply With Quote #4

in computer.h CHardDrive has a constructor defined as

Code:
CHardDrive(char *name, int size );
But you don't have the actual constructor function anywhere..

You need something like (in computer.cpp)

Code:
CHardDrive::CHardDrive(char *name, int size)
{
  m_size = size;
  m_name = name;
}
pRED* is offline
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 08-29-2007 , 20:02   Re: C++ help
Reply With Quote #5

Quote:
Originally Posted by pRED* | NZ View Post
in computer.h CHardDrive has a constructor defined as

Code:
CHardDrive(char *name, int size );
But you don't have the actual constructor function anywhere..

You need something like (in computer.cpp)

Code:
CHardDrive::CHardDrive(char *name, int size)
{
  m_size = size;
  m_name = name;
}

Line 60 of the computer.cpp ,, youll see the HardDrive::HardDrive function there already with basicly the same thing you have lol
Peoples Army is offline
pRED*
Join Date: Dec 2006
Old 08-29-2007 , 20:09   Re: C++ help
Reply With Quote #6

Oh.. Man that indenting is annoying.

Well thats what the error means. I think. Unresolved external sybmol = defined header prototype but no actual function body.
pRED* is offline
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 08-29-2007 , 20:14   Re: C++ help
Reply With Quote #7

Quote:
Originally Posted by pRED* | NZ View Post
Oh.. Man that indenting is annoying.

Well thats what the error means. I think. Unresolved external sybmol = defined header prototype but no actual function body.
yeah for some reason any time i copy and paste a code into code breackets it squishes it togther .
Peoples Army is offline
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 08-30-2007 , 18:50   Re: C++ help
Reply With Quote #8

no body knows why ??
Peoples Army is offline
pRED*
Join Date: Dec 2006
Old 08-30-2007 , 19:39   Re: C++ help
Reply With Quote #9

You're missing the constructor function for CComponent::CComponent()

(Yes I know you have the name version..)
pRED* is offline
Peoples Army
SourceMod Donor
Join Date: Mar 2007
Old 08-31-2007 , 09:21   Re: C++ help
Reply With Quote #10

Quote:
Originally Posted by pRED* | NZ View Post
You're missing the constructor function for CComponent::CComponent()

(Yes I know you have the name version..)

if you dont have a defualt constructor , then you get an error . so if you want to use a constructor with paramters , you still need a defualt constructor . wether it actually points to a function or not is inconsequencial .
Peoples Army is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:53.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode