-
Invalid use of class
I have to create two classes CPU and Computer. The CPU.h and Computer.h needs to be different files than the CPU.cpp and Computer.cpp. I believe I created my files correctly however in my Computer constructor, I'm told to "allocates memory for the pointer of of CPU (i.e., call the constructor of the CPU class)." I'm not sure if I'm doing that right because I get the error 13 invalid use of `class CPU' , I bolded the line which it happens on
Heres Computer.cpp:
Code:
//File Computer.cpp
#include <string>
#include<iostream>
#include "CPU.h"
#include "Computer.h"
Computer:: Computer()
{
brandName = "?";
memory = 0;
price = 0.0;
*cpu->CPU();
}
Computer:: ~Computer()
{
cout<<"The computer "<<brandName<<" is getting destroyed."<<endl;
free(cpu);
}
string Computer:: getBrandName()
{
return brandName;
}
CPU * Computer:: getCPU()
{
return cpu;
}
int Computer:: getMemory()
{
return memory;
}
double Computer:: getPrice()
{
return price;
}
void Computer:: setBrandName(string BrandName)
{
brandName = BrandName;
}
void Computer:: setCPU(string cpuType, int cpuSpeed)
{
cpu->setType(cpuType);
cpu->setSpeed(cpuSpeed);
}
void Computer:: setMemory(int memoryAmount)
{
memory = memoryAmount;
}
void Computer:: setPrice(double price)
{
price = price;
}
void Computer:: printInfo()
{
cout<<"\nBrandName:\t"<<brandName<<"\nCPU:\t\t"<<cpu->getType()<<","
<<cpu->getSpeed()<<"HZ\nMemory:\t\t"<<memory<<"M\nPrice:\t\t$"<<price
<<"\n\n";
}
Heres Computer.h:
Code:
//File Computer.h
#include <string>
#include "CPU.h"
//#include <iostream>
using namespace std;
#ifndef Computer_H
#define Computer_H
class Computer
{
private:
string brandName;
CPU * cpu;
int memory;
double price;
public:
Computer();
~Computer();
string getBrandName();
CPU* getCPU();
int getMemory();
double getPrice();
void setBrandName(string);
void setCPU(string, int);
void setMemory(int);
void setPrice(double);
void printInfo();
};
#endif
Heres CPU.h:
Code:
// File CPU.h
#include <string>
using namespace std;
#ifndef CPU_H
#define CPU_H
class CPU
{
private:
string type;
int speed;
public:
CPU();
~CPU();
string getType();
int getSpeed();
void setType(string);
void setSpeed(int);
void printInfo();
};
#endif
And heres CPU.cpp
Code:
// File CPU.cpp
#include <string>
#include <iostream>
#include "CPU.h"
CPU:: CPU()
{
type = "?";
speed = 0;
}
CPU:: ~CPU()
{
cout<<"CPU with the type "<< type <<" and the speed "<< speed <<" is being destroyed." <<endl;
}
string CPU:: getType()
{
return type;
}
int CPU:: getSpeed()
{
return speed;
}
void CPU:: setType(string type)
{
type = type;
}
void CPU:: setSpeed(int speed)
{
speed = speed;
}
void CPU:: printInfo()
{
cout<<type<<","<<speed<<"HZ"<<endl;
}
Any help would be appreciated
-
First of all, don't use 'free' (for now, anyway), as it won't call the destructor on the object. You should allocate with 'new' and deallocate with 'delete' (eventually, you'll need to get a firm grasp of RAII, but for now we'll just leave it that).
Anyway, a pointer is just an address, and right now your 'cpu' variable points to nowhere in particular. Again, you'll need to use 'new' to allocate some memory for it, and then assign that directly to 'cpu'. The 'new' expression takes any valid arguments to the type's constructor, and for an object of type 'CPU', no arguments are needed. The syntax/idiom you should be using is similar to this:
Code:
std::string* s = new std::string( "I'm alive!" );
// ...do something...
delete s; // calls ~std::string() and releases memory
-
Also, do not call the constructors or destructors directly unless you are sure what you are doing, and in this case, you are not.
-
Are you guys in the same class?