Thanks to the help I recieved earlier, I figured out how to make my other header file and my other .cpp file!! So thank you for that. I am now trying to compile all of my files together, and am getting quite a few linker errors, even though the functions that it is complaining about all exist and have definitions. Can someone help me figure out why I am getting all these errors:
[Linker error] undefined reference to `Computer::Computer()'
[Linker error] undefined reference to `Computer::setBrandName(std::string)'
[Linker error] undefined reference to `Computer::setPrice(double)'
[Linker error] undefined reference to `Computer::setMemory(int)'
and so on. I am new to C++, so this may be something really easy to fix, I'm just stumped about what else I need to do here.
Here are my five files (the two headers, their two associated cpp files, and the main file, called Assignment6.cpp):
// CPU.h
#ifndef CPU_H
#define CPU_H
using namespace std;
class CPU
{
private:
string type;
int speed;
public:
CPU();
~CPU();
string getType();
int getSpeed();
void setType(string type);
void setSpeed(int speed);
void printInfo();
};
#endif
// CPU.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "CPU.h"
using namespace std;
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 newType)
{ type = newType; }
void CPU::setSpeed(int newSpeed)
{ speed = newSpeed; }
void CPU::printInfo()
{ cout << type << "," << speed << endl; }
// Computer.h
#ifndef Computer_H
#define Computer_H
using namespace std;
class CPU;
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 newBrandName);
void setCPU (string cpuType, int cpuSpeed);
void setMemory (int memoryAmount);
void setPrice (double newPrice);
void printInfo();
};
#endif
// Computer.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "Computer.h"
#include "CPU.h"
using namespace std;
Computer::Computer()
{
brandName = "?";
*cpu = CPU();
memory = 0;
price = 0.0;
}
Computer::~Computer()
{
delete cpu;
cout << "The computer " << brandName << " is being destroyed." << endl;
}
string Computer::getBrandName()
{ return brandName; }
CPU * Computer::getCPU()
{ return cpu; }
int Computer::getMemory()
{ return memory; }
double Computer::getPrice()
{ return price; }
void Computer::setBrandName(string newBrandName)
{ brandName = newBrandName; }
void Computer::setCPU (string cpuType, int cpuSpeed)
{
}
void Computer::setMemory (int memoryAmount)
{ memory = memoryAmount; }
void Computer::setPrice (double newPrice)
{ price = newPrice; }
void Computer::printInfo()
{ cout << "\nBrandName:\t" << brandName << "\nCPU:\t\t" << cpu << "\nMemory:\t\t" << memory << "\nPrice:\t\t" << price << "\n\n" << endl; }
// Assignment #: 6
// Description: This program displays a menu of choices to a user
// and performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is entered.
#include <iostream>
#include <string>
#include <cstdlib>
#include "CPU.h"
#include "Computer.h"
using namespace std;
void printMenu();
int main()
{
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
string inputInfo;
string brandName;
double price;
int memory;
string cpuType;
int cpuSpeed;
string line;
// allocate memory for the pointer of the Computer
Computer * computer1 = new Computer();
printMenu();
do // will ask for user input
{
cout << "What action would you like to perform?\n";
input1 = getchar();
input1 = toupper(input1);
// matches one of the case statement
switch (input1)
{
case 'A': //Add Computer
cout << "Please enter the computer information:\n";
cout << "Enter a brand name:\n";
cin >> brandName;
computer1->setBrandName(brandName);
cout << "Enter a computer price:\n";
cin >> price;
computer1->setPrice(price);
cout << "Enter a computer memory:\n";
cin >> memory;
computer1->setMemory(memory);
cout << "Enter a cpu type:\n";
cin >> cpuType;
cout << "Enter a cpu speed:\n";
cin >> cpuSpeed;
computer1->setCPU(cpuType, cpuSpeed);
break;
case 'D': //Display computer
computer1->printInfo();
break;
case 'Q': //Quit
delete computer1;
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action\n";
break;
}
getchar(); //to flush '\n'
} while (input1 != 'Q');
return 0;
}
/** The method printMenu displays the menu to a user**/
void printMenu()
{
cout << "Choice\t\tAction\n";
cout << "------\t\t------\n";
cout << "A\t\tAdd Computer\n";
cout << "D\t\tDisplay Computer\n";
cout << "Q\t\tQuit\n";
cout << "?\t\tDisplay Help\n\n";
}