Author
|
Message
|
SourceMod Donor
|
08-29-2007
, 18:53
C++ help
|
#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.
|
|
|
|