g++ Undefined reference to base class destructor and derived class pointer - c++

Edit: I had forgotten to explicitly define my class destructors in their respective .cpp files. I replaced *p with string *killList = new string[10];
and my code now compiles. Thanks for your replies!
I've tried to compile the following files using the command :
g++ -o hunter hunter_h.h hunter_h.cpp animal_h.h animal_h.cpp main.cpp
animal_h.h
#include <iostream>
#include <string>
#ifndef ANIMAL_H
#define ANIMAL_H
using namespace std;
// Animal class
class animal
{
friend class hunter;
// need a name, species, private ID
public:
animal();
animal(string aSpecies);
string name;
string species;
string getSpecies();
void setName(string aName);
string getName();
int getID();
~animal();
private:
static int uID;
};
#endif
animal_h.cpp
#include "animal_h.h"
//#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int animal:: uID = 0 ;
animal::animal()
{
cout << "Created an animal!" << endl;
name = "?";
species = "?";
uID++;
}
animal::animal(string aSpecies)
{
cout << "Created 1 "<< aSpecies << "!" << endl;
name= "?";
species = aSpecies;
uID++;
}
string animal::getSpecies()
{
cout << species << endl;
}
void animal::setName(string aName)
{
name = aName;
cout << "This " << species << " is now called " << name << endl;
}
string animal::getName()
{
cout << name << endl;
}
int animal:: getID()
{
cout << uID << endl;
}
hunter_h.h This is a derived class of the animal base class with unique behaviors.
#include "animal_h.h"
#include <iostream>
#ifndef ANIMAL_HUNTER
#define ANIMAL_HUNTER
class hunter : public animal
{
public:
hunter();
hunter(std::string aSpecies);
void recordKills(std::string kill);
static int tKills;
int totalKills();
static std::string *theKills();
static std::string *p;
static int clearTotal();
~hunter();
};
#endif
hunter_h.cpp
#include "animal_h.h"
#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int hunter:: tKills =0;
string killList[10];
hunter::hunter()
{
cout<<"created a hunter! "<<endl;
name= "? ";
species="? ";
string *p;
p = &killList[0];
}
hunter::hunter(string aSpecies)
{
name = "?";
species = aSpecies;
cout << "created a hunting "<<species <<endl;
}
string *theKills()
{
return hunter::p;
}
void hunter::recordKills(string kill)
{
cout << kill << " killed." << endl;
*(p+tKills) = kill;
tKills++;
cout << tKills << " total kills." << endl;
}
int hunter::totalKills()
{
cout << name << "'s " << "Total kills: " << tKills << endl;
}
int hunter::clearTotal()
{
delete[] killList;
return 0;
}
main.cpp
#include "animal_h.h"
#include "hunter_h.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
hunter *hunterC;
hunterC= new hunter("cheetah");
hunterC->recordKills("Mouse");
hunterC-> recordKills("Gazelle, Gazelle");
hunterC-> recordKills("Hyena");
hunterC-> recordKills("Rabbit, Rabbit");
hunterC->theKills;
hunterC->clearTotal;
}
Now, when I try to compile I get the following warning and errors:
hunter_h.cpp: In static member function ‘static int hunter::clearTotal()’:
hunter_h.cpp:49:11: warning: deleting array ‘killList’
delete[] killList;
^
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x71): undefined reference to `animal::~animal()'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x101): undefined reference to `animal::~animal()'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x119): undefined reference to `hunter::p'
/tmp/ccnv8xdj.o:hunter_h.cpp:(.text+0x15a): undefined reference to `hunter::p'
/tmp/ccqCD1e7.o:main.cpp:(.text+0x1f4): undefined reference to `animal::~animal()'
/tmp/ccqCD1e7.o:main.cpp:(.text+0x20c): undefined reference to `animal::~animal()'
collect2: error: ld returned 1 exit status
I've been learning C++ for a couple months only so am not sure where the above code is going wrong. How can I get this to compile and run?

killList is not allocated using new[], then dont delete it with delete[]. It is not allocated runtime, then it does not have to be deallocated explicitly. It will release its memory when the program exits. With your code as it is you are running the chance of overrunning you killList array.
Try using std::vector instead.
std::vector<std::string> killList;
...
void recordKills(std::string s)
{
...
killList.push_back(s);
}
void clearTotal()
{
killList.clear();
}

Related

class header: error: expected unqualified-id before 'char' while compiling

Sorry for bad english.
I am new on c++ and trying to understand to send references to class object.
But I get above error. If I use const char then error comes up for 'const' part.
Here is my code:
main:
#include <iostream>
#include "DelMe-ClassHeader.h"
using namespace std;
int main() {
char var1 [2];
int var2;
for (int i = 0; i < 2; i++) {
var1[i] = 2;
}
int var2 = 0;
tc = TestClass(var1, var2);
cout << "before tc.changeValue" << endl;
cout << "var1 is " << var1 << endl;
cout << "var2 is " << var2 << endl;
tc.changeValue()
cout << "before tc.changeValue" << endl;
cout << "var1 is " << var1 << endl;
cout << "var2 is " << var2 << endl;
}
header:
#ifndef TestClass
#define TestClass
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass(char (& first)[2]}, int& second);
void changeValue ();
private:
char (& privArray)[2];
int& privInt;
};
#endif
cpp:
#include "DelMe-ClassHeader.h"
#include <iostream>
using namespace std;
TestClass::TestClass(char (& first)[15], int& second) {
this->priveArray = first;
this->privInt = second;
}
void TestClass::changeValue () {
privInt = atoi(privArray);
}
and the error is:
E:\Programing\CodeBlocks\Cpp\DelMe\DelMe\DelMe-ClassHeader.h|10|error: expected unqualified-id before 'char'
I gratefull for any help
The header contains two errors:
#define TestClass defines TestClass as an empty string. Therefore all occurences of TestClass will be replaced with an empty string, hence the errors. For more information read about the C++ preprocessor.
There is an extra } in the parameter list of TestClass.
Replace with this:
#ifndef TestClass_h_inc_
#define TestClass_h_inc_
#include <iostream>
using namespace std;
class TestClass {
public:
TestClass(char(&first)[2], int& second);
void changeValue();
private:
char(&privArray)[2];
int& privInt;
};
#endif

undefined reference to `Class::function()' error in c++ [CLion ide]

I am trying to practice object oriented programming structure in c++. I have written some piece of code. But I got an undefined reference error.
I've tried to build with clion ide but it gave me error. I've tried to compile it on linux terminal using g++ command but it gave me same error.
Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
public:
Account ();
Account(double init_balance);
double getBalance();
void deposit(double amt);
void withdraw(double amt);
private:
double balance;
};
#endif // ACCOUNT_H
Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
Account::Account(double init_balance)
:balance(init_balance)
{
}
double Account::getBalance(){
return balance;
}
void Account::deposit(double amt){
balance += amt;
}
void Account::withdraw(double amt){
balance -= amt;
}
Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "Account.h"
#include <iostream>
#include <string>
using namespace std;
class Customer
{
public:
Customer(string first_Name, string last_Name) :
firstName(first_Name), lastName(last_Name)
{
}
//Customer(string first_Name, string last_Name);
string getFirstName();
string getLastName();
Account getAccount();
void setAccount(Account acc);
private:
string firstName;
string lastName;
Account account;
};
#endif // CUSTOMER_H
Customer.cpp
#include "Customer.h"
#include <iostream>
#include <string>
using namespace std;
string Customer::getFirstName(){
return firstName;
}
string Customer::getLastName(){
return lastName;
}
Account Customer::getAccount(){
return account;
}
void Customer::setAccount(Account acc)
{
account = acc;
}
main.cpp
#include "Account.h"
#include "Customer.h"
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
Customer *customer;
Account account(0.0);
// Create an account that can has a 500.00 balance.
cout << endl << "Creating the customer Jane Smith.";
customer = new Customer("Jane", "Smith");
cout << endl << "Creating her account with a 500.00 balance.";
customer->setAccount(Account(500.00));
account = customer->getAccount();
cout << endl << "Withdraw 150.00";
account.withdraw(150.00);
cout << endl << "Deposit 22.50";
account.deposit(22.50);
cout << endl << "Withdraw 47.62";
account.withdraw(47.62);
// Print out the final account balance
cout << endl
<< "Customer ["
<< customer->getLastName()
<< ", "
<< customer->getFirstName()
<< "] has a balance of "
<< account.getBalance()
<< endl;
delete customer;
Account acc1(100), acc2(200);
double suAcc = acc1.getBalance()+ acc2.getBalance();
Account sumAcc(suAcc);
cout << "Balance of sumAcc is " << sumAcc.getBalance() << endl;
return (EXIT_SUCCESS);
}
Here is the error:
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:9: undefined reference to `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:9:(.text+0x2f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15: undefined reference to `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15:(.text+0x12e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Account::Account(double)'
/cygdrive/c/Users/James/CLionProjects/untitled13/main.cpp:15: undefined reference to `Customer::setAccount(Account)
'
It goes on with every method.
I was able to compile this after making several changes.
Remove using namespace std from customer.h. It's bad practice to use "using namespace" here as it could cause name collisions when another file includes customer.h. As a result of removing "using namespace std" you'll need to qualify all of your strings as std::string instead of string.
You declared a default constructor for Account but there is no implementation provided. When a customer is created it tries to call the default constructor of account. If you are on a newer version of c++ you can change this line in your header file.
Account();
to
Account() = default;
If you're on an older version add this to Account.cpp
Account(){}

C++ clang linker command failed with exit code 1 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
My professor gave me two class header and .cpp files to build on. When I include these in main, they work fine. Whenever I just use his files, I get linker errors with clang and xcode.
Here's the error:
shannigan#mbp-007100 inheritance (master) $ make main
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"SavitchEmployees::SalariedEmployee::SalariedEmployee()", referenced from:
_main in main-0d7e27.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Here's my main:
#include "employee.h"
#include "salariedemployee.h"
#include <string>
#include <cstdlib>
#include <iostream>
using namespace SavitchEmployees;
using namespace std;
int main() {
cout << "Do I run?" << endl;
SalariedEmployee sam;
return 0;
};
The header file for Employee:
//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;
namespace SavitchEmployees
{
class Employee
{
public:
Employee( );
Employee(const string& theName, const string& theSsn);
string getName( ) const;
string getSsn( ) const;
double getNetPay( ) const;
void setName(const string& newName);
void setSsn(const string& newSsn);
void setNetPay(double newNetPay);
void printCheck( ) const;
protected:
string name;
string ssn;
double netPay;
};
}//SavitchEmployees
#endif //EMPLOYEE_H
The CPP file for main:
//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h.
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::string;
using std::cout;
namespace SavitchEmployees
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), netPay(0)
{
//deliberately empty
}
Employee::Employee(const string& theName, const string& theNumber)
: name(theName), ssn(theNumber), netPay(0)
{
//deliberately empty
}
string Employee::getName( ) const
{
return name;
}
string Employee::getSsn( ) const
{
return ssn;
}
double Employee::getNetPay( ) const
{
return netPay;
}
void Employee::setName(const string& newName)
{
name = newName;
}
void Employee::setSsn(const string& newSsn)
{
ssn = newSsn;
}
void Employee::setNetPay (double newNetPay)
{
netPay = newNetPay;
}
void Employee::printCheck( ) const
{
cout << "\nERROR: printCheck FUNCTION CALLED FOR AN \n"
<< "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
<< "Check with the author of the program about this bug.\n";
exit(1);
}
}//SavitchEmployees
SalariedEmployees header:
//This is the header file salariedemployee.h.
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using std::string;
namespace SavitchEmployees
{
class SalariedEmployee : public Employee
{
protected:
double salary;//weekly
public:
SalariedEmployee( );
SalariedEmployee (const string& theName, const string& theSsn,
double theWeeklySalary);
double getSalary( ) const;
void setSalary(double newSalary);
void printCheck( );
};
}//SavitchEmployees
#endif //SALARIEDEMPLOYEE_H
SalariedEmployee.cpp:
//This is the file salariedemployee.cpp
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the header file salariedemployee.h.
#include <iostream>
#include <string>
#include "salariedemployee.h"
using std::string;
using std::cout;
using std::endl;
namespace SavitchEmployees
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(const string& newName, const string& newNumber,
double newWeeklyPay)
: Employee(newName, newNumber), salary(newWeeklyPay)
{
//deliberately empty
}
double SalariedEmployee::getSalary( ) const
{
return salary;
}
void SalariedEmployee::setSalary(double newSalary)
{
salary = newSalary;
}
void SalariedEmployee::printCheck( )
{
setNetPay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << getName( ) << endl;
cout << "The sum of " << getNetPay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << getSsn( ) << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
}//SavitchEmployees
How can I get rid of these linker errors so I can focus on my actual code? Is there anything obvious wrong? The only thing I've changed was making the "private" variables protected.
I can't see the class named SalariedEmployee.
I think the main function should look like this.
int main() {
cout << "Do I run?" << endl;
Employee sam;
return 0;
};
You have to use Employee instead of SalariedEmployee

Linker error undefined reference to class::class (Person::Person in my case)

I am getting a linker error undefined reference to Person::Person when trying to implement my program. The three parts are below. I have been working on fixing it for a few hours now. I know it's probably something simple that I am just not seeing. But I have looked around on the internet and still have not found my answer. So any help would be appreciated.
#ifndef PERSON0_H_
#define PERSON0_H_
#include <string>
class Person // class declaration
{
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person() {lname = ""; fname[0] = '\0';}
Person(const std::string & ln, const char * fn = "Hay you");
void Show() const;
void FormalShow() const;
};
#endif
#include <iostream>
#include <string>
#include "person0.h"
void Person::Show() const
{
using namespace std;
std::cout << fname << " " << lname << '\n';
}
void Person::FormalShow() const
{
using std::cout;
std::cout << lname << ", " << fname << '\n';
}
#include <iostream>
#include <string>
#include "person0.h"
int main()
{
using namespace std;
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
cout << endl;
one.FormalShow();
cout << endl;
two.Show();
cout << endl;
two.FormalShow();
cout << endl;
three.Show();
cout << endl;
three.FormalShow();
cin.get();
cin.get();
return 0;
}
I am not really a C++ person, so the terminology might be wrong, but I would say that the implementation of the
Person::Person(const std::string & ln, const char * fn)
constructor is missing.

Compiling class issue

Keep getting these errors while trying to compile an c++ class program.
testStock.cpp: In function ‘int main()’: testStock.cpp:8: error:
‘Stock’ was not declared in this scope testStock.cpp:8: error:
expected ;' before ‘first’ testStock.cpp:9: error: ‘first’ was not
declared in this scope testStock.cpp:12: error: expected;' before
‘second’ testStock.cpp:13: error: ‘second’ was not declared in this
scope
stock.h
#ifndef STOCK_H
#define STOCK_H
using namespace std;
class Stock
{
private:
string symbol;
string name;
double previousClosingPrice;
double currentPrice;
public:
Stock(string symbol, string name);
string getSymbol() const;
string getName() const;
double getPreviousClosingPrice() const;
double getCurrentPrice() const;
double changePercent();
void setPreviousClosingPrice(double);
void setCurrentPrice(double);
};
#endif
stock.cpp
#include <string>
#include "stock.h"
Stock::Stock(string symbol, string name)
{
this->symbol = symbol;
this->name = name;
}
string Stock::getSymbol() const
{
return symbol;
}
string Stock::getName() const
{
return name;
}
void Stock::setPreviousClosingPrice(double closing)
{
previousClosingPrice = closing;
}
void Stock::setCurrentPrice(double current)
{
currentPrice = current;
}
double Stock::getPreviousClosingPrice() const
{
return previousClosingPrice;
}
double Stock::getCurrentPrice() const
{
return currentPrice;
}
double Stock::changePercent()
{
return ((currentPrice - previousClosingPrice)/previousClosingPrice) * 100;
}
testStock.cpp
#include <string>
#include <iostream>
#include "string.h"
using namespace std;
int main()
{
Stock first("aapl", "apple");
cout << "The stock symbol is " << first.getSymbol() << " and the name is " << first.getName() << endl;
first.setPreviousClosingPrice(130.0);
first.setCurrentPrice(145.0);
Stock second("msft", "microsoft");
second.setPreviousClosingPrice(30.0);
second.setCurrentPrice(33.0);
first.changPercent();
second.changePercent();
cout << "The change in percent for " << first.getName << " is " << first.changePercent() << endl;
cout << "The change in percent for " << second.getName << " " << second.getSymbol() << " is " << second.changePercent() << endl;
return 0;
}
Im sure its something obvious but its only my second class program.
It looks like you have omitted
#include "stock.h"
from your testStock.cpp.
Compiler tells you "‘Stock’ was not declared in this scope ". So you should ask yourself "Where is 'Stock' declared?" and you should be able to answer it: "It's declared in stock.h".
And "Why compiler doesn't know that 'Stock' is declared in stock.h?" Because you haven't included it. So as it was mentioned here already, #include "stock.h" is the solution.
Hope you will spend more time reading compilers errors / warnings and also more time trying to understand them ;)
You are just not including "stock.h" in your main file, so the compiler doesn't know what Stock first means.
#include "stock.h"
and you will be able to create Stock object as it will be visible to your TestStock class.