链接器错误 - 未定义的参考C ++

时间:2016-03-19 16:18:35

标签: c++ linker

所以我一直在研究一些专注于多态性的代码,在尝试链接文件时,我得到以下链接器错误:

main.cpp :(。text + 0x1e7):未定义引用`InstructionalAsst :: InstructionalAsst(std :: string,int,double)'

main.cpp :(。text + 0x780):未定义引用`Employee :: getName()'

collect2:错误:ld返回1退出状态

我知道这个问题已经被问了很多,我做了一些研究,我相信我明白了问题是什么(我没有正确定义构造函数)但是在查看代码时看起来我已经拥有了所有的构造函数我使用正确定义。我现在处于亏损状态,所以如果有人能看一眼并向我展示我的方式的错误,那将非常感激。提前谢谢!

的main.cpp

#include "std_lib_facilities.h"
#include "staff.h"
#include "employee.h"
#include "InstructionalAsst.h"
#include <sstream>
int main()
{

    //Create a Employee Vector
    vector<Employee*> employees; //Polymorphic Container
    string line;
    ifstream file("employee.txt");


    while(getline(file, line)) // Keeps going while lines are available.
    {

        istringstream iss(line);
        vector<string> info;
        string place;
        while(iss >> place)
        {
            info.push_back(place);
            cout << place << endl;
        }

        if(info.at(0) == "IA")
        {

            employees.push_back(new InstructionalAsst(info.at(1), stoi(info.at(2)), stod(info.at(3))));

        }
    }

    file.close();

    //Go through vector printing out name and total salary

    for(int i=0; i< employees.size(); ++i)
    {
        cout << "Name:" << employees.at(i)->getName() << "Salary:" << employees.at(i)->salary() << endl;
    }

    return 0;
}

employee.h

#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED

#include "std_lib_facilities.h"

//Employee Header file

class Employee
{

public:
    Employee(string fullName, int years);
    string getName();
    int getYears();
    virtual double salary() = 0;
    virtual string info() = 0;

protected:
    string e_fullName;
    int e_years;
};

#endif //EMPLOYEE_H_INCLUDED

employee.cpp

#include "std_lib_facilities.h"
#include "employee.h"

//Employee source file

Employee::Employee(string fullName, int years)
{
    e_fullName = fullName;
    e_years = years;
}

string Employee::getName()
{
    return e_fullName;
}

int Employee::getYears()
{
    return e_years;
}

staff.h

#ifndef STAFF_H_INCLUDED
#define STAFF_H_INCLUDED

#include "std_lib_facilities.h"
#include "employee.h"

// Staff Header File

class Staff : public Employee
{
public:
    Staff(string fullName, int years, double sal);
    double salary();
    string info();
protected:
    double s_base_salary;

};

#endif //STAFF_H_INCLUDED

staff.cpp

#include "std_lib_facilities.h"
#include "staff.h"

// This is the Staff source file

Staff::Staff(string fullName, int years, double sal) : Employee(fullName, years)
{
    s_base_salary = sal;
}

double Staff::salary()
{
    return s_base_salary;
}

string Staff::info()
{
    return "Name: " +  getName() + "\nYears of service: " + to_string(getYears()) +  "\nSalary: " + to_string(salary());
}

InstructionalAsst.h

#ifndef INSTRUCTIONALASST_H_INCLUDED
#define INSTRUCTIONALASST_H_INCLUDED

#include "std_lib_facilities.h"
#include "staff.h"

//InstructionalAsst Header File

class InstructionalAsst : public Staff
{
public:
    InstructionalAsst(string fullName, int years, double sal);
    double salary();
    string info();
};

#endif //INSTRUCTIONALASST_H_INCLUDED

InstructionalAsst.cpp

#include "std_lib_facilities.h"
#include "InstructionalAsst.h"

// InsturctionalAsst Source File

InstructionalAsst::InstructionalAsst(string fullName, int years, double sal) : Staff(fullName, years, sal)
{
}

double InstructionalAsst::salary()
{
    return Staff::salary();
}


string InstructionalAsst::info()
{
    return "Name: " +  getName() + "\nYears of service: " + to_string(getYears()) + "\n Salary: " + to_string(salary());
}

std_lib_facilities.h

/*
 std_lib_facilities.h
 */

/*
 simple "Programming: Principles and Practice using C++ (second edition)" course header to
 be used for the first few weeks.
 It provides the most common standard headers (in the global namespace)
 and minimal exception/error support.

 Students: please don't try to understand the details of headers just yet.
 All will be explained. This header is primarily used so that you don't have
 to understand every concept all at once.

 By Chapter 10, you don't need this file and after Chapter 21, you'll understand it

 Revised April 25, 2010: simple_error() added

 Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
 Revised November 28 2013: add a few container algorithms
 Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
 */

#ifndef H112
#define H112 251113L


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <list>
#include <forward_list>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <array>
#include <regex>
#include <random>
#include <stdexcept>

//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

using Unicode = long;

//------------------------------------------------------------------------------

using namespace std;

template<class T> string to_string(const T& t)
{
    ostringstream os;
    os << t;
    return os.str();
}

struct Range_error : out_of_range { // enhanced vector range error reporting
    int index;
    Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
};


// trivially range-checked vector (no iterator checking):
template<class T> struct Vector : public std::vector<T> {
    using size_type = typename std::vector<T>::size_type;

#ifdef _MSC_VER
    // microsoft doesn't yet support C++11 inheriting constructors
    Vector() { }
    explicit Vector(size_type n) :std::vector<T>(n) { }
    Vector(size_type n, const T& v) :std::vector<T>(n,v) { }
    template <class I>
    Vector(I first, I last) : std::vector<T>(first, last) { }
    Vector(initializer_list<T> list) : std::vector<T>(list) { }
#else
    using std::vector<T>::vector; // inheriting constructor
#endif

    T& operator[](unsigned int i) // rather than return at(i);
    {
        if (i < 0 || this->size() <= i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
    const T& operator[](unsigned int i) const
    {
        if (i < 0 || this->size() <= i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
    using size_type = std::string::size_type;
    //  using string::string;

    char& operator[](unsigned int i) // rather than return at(i)
    {
        if (size() <= i) throw Range_error(i);
        return std::string::operator[](i);
    }

    const char& operator[](unsigned int i) const
    {
        if (size() <= i) throw Range_error(i);
        return std::string::operator[](i);
    }
};


namespace std {

    template<> struct hash<String>
    {
        size_t operator()(const String& s) const
        {
            return hash<std::string>()(s);
        }
    };

} // of namespace std


struct Exit : runtime_error {
    Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
    throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
    error(s + s2);
}

inline void error(const string& s, int i)
{
    ostringstream os;
    os << s << ": " << i;
    error(os.str());
}


template<class T> char* as_bytes(T& i)  // needed for binary I/O
{
    void* addr = &i;  // get the address of the first byte
    // of memory used to store the object
    return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
    return;
}

inline void keep_window_open(string s)
{
    if (s=="") return;
    cin.clear();
    cin.ignore(120,'\n');
    for (;;) {
        cout << "Please enter " << s << " to exit\n";
        string ss;
        while (cin >> ss && ss!=s)
            cout << "Please enter " << s << " to exit\n";
        return;
    }
}


// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)  // write ``error: s and exit program
{
    cerr << "error: " << s << '\n';
    keep_window_open();   // for some Windows environments
    exit(1);
}

// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max


// run-time checked narrowing cast (type conversion). See ???.
template<class R, class A> R narrow_cast(const A& a)
{
    R r = R(a);
    if (A(r)!=a) error(string("info loss"));
    return r;
}

// random number generators. See 24.7.



inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }

inline int randint(int max) { return randint(0, max); }

//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x

// container algorithms. See 21.9.

template<typename C>
using Value_type = typename C::value_type;

template<typename C>
using Iterator = typename C::iterator;

template<typename C>
// requires Container<C>()
void sort(C& c)
{
    std::sort(c.begin(), c.end());
}

template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
    std::sort(c.begin(), c.end(), p);
}

template<typename C, typename Val>
// requires Container<C>() && Equality_comparable<C,Val>()
Iterator<C> find(C& c, Val v)
{
    return std::find(c.begin(), c.end(), v);
}

template<typename C, typename Pred>
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
Iterator<C> find_if(C& c, Pred p)
{
    return std::find_if(c.begin(), c.end(), p);
}


// Initialize C++11 unique_ptr, at least until std::make_unique()
// becomes part of the standard (C++14)
template <typename Value, typename ... Arguments>
std::unique_ptr<Value> make_ptr(Arguments && ... arguments_for_constructor)
{
    return std::unique_ptr<Value>(
                                  new Value(std::forward<Arguments>(arguments_for_constructor)...)
                                  );
}

#endif //H112

employee.txt(测试样本)

IA Joe_Blow 15 4000
LI Jane_Doe 25 6000 500
PF Henny_Penny 5 15 1025
IA Gnarly_Moe 15 5000
DH TaskMaster 5 10 1000 750
PF Holey_Moley 10 50 4025
IA John_Doe 10 4750
PF Jack_Black 2 5 500

0 个答案:

没有答案