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";
}

Recommended Answers

All 4 Replies

What compiler are you using (if you already told us in another thread then tell us again because I don't want to have to read all your other posts to find out). And how are you linking all those files together ? Are you doing command-line or IDE builds ?

What compiler are you using (if you already told us in another thread then tell us again because I don't want to have to read all your other posts to find out). And how are you linking all those files together ? Are you doing command-line or IDE builds ?

I'm using Dev-C++. I know its outdated, but last time I downloaded VS it totally screwed up the computer I was using and I had to get a new hard drive, so I really don't want to mess with it again.

I'm not sure about what you mean about how I'm linking them together, except for the include calls at the beginning of the files. I just have them all in the same folder on my computer, and I compile using the IDE, so I'm guessing that's using IDE Builds, but I'm not sure at all that that is correct.

Sorry about my lack of knowledge about this and the terminology. This is my first C++ program, and I'm still really feeling my way around it.

>>I'm using Dev-C++. I know its outdated, but last time I downloaded VS it totally screwed up the computer I was using and I had to get a new hard drive, so I really don't want to mess with it again.

??? Never heard of that before

In Dev-C++ you should have first created a project by selecting File --> New --> Project, then in the dialog box select Console Application. After the IDE finishes creating the project you can add the *.cpp and *.h files to the project.

>>I'm not sure about what you mean about how I'm linking them together,
When you have several *.cpp files they each have to be compiled into object files (your compiler will create *.o files). After that the linker has to put all those *.o files together along with the libraries (*.a) in order to create the executable program. Most (if not all) of the libraries you will be using for the forseeable future will be supplied by your compiler.

>>I'm using Dev-C++. I know its outdated, but last time I downloaded VS it totally screwed up the computer I was using and I had to get a new hard drive, so I really don't want to mess with it again.

??? Never heard of that before

Yeah, I think something go messed up with the .NET framework when I installed VS, but overall it just made the computer not boot and crashed the hard drive. Not a fun situation, especially when you are trying to figure out how to do the first homework assignment for a new class and you really need the computer, heh heh.

In Dev-C++ you should have first created a project by selecting File --> New --> Project, then in the dialog box select Console Application. After the IDE finishes creating the project you can add the *.cpp and *.h files to the project.

>>I'm not sure about what you mean about how I'm linking them together,
When you have several *.cpp files they each have to be compiled into object files (your compiler will create *.o files). After that the linker has to put all those *.o files together along with the libraries (*.a) in order to create the executable program. Most (if not all) of the libraries you will be using for the forseeable future will be supplied by your compiler.

Oh, okay, I didn't realize that I had to do it all as a project. I just have the files all in the same folder, and I thought that would do it. I created a new project, copied everything over, and of course it works!

Thank you for helping me to understand the basic structure of this stuff, AncientDragon. I think I'm starting to get it, but it will take awhile, I'm sure! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.