链接文件以构建EXE时出现C ++错误

时间:2011-11-10 00:20:32

标签: c++ linker

我目前正处于C ++和数据结构课程中,但对于我的生活,我无法弄清楚该怎么办我正在做的这个问题。该程序编译,但当我尝试链接文件以构建.exe时,我收到以下错误:

prog9.o: In function `main':
/home/csci340/z1615629/340/assign_9/prog9.cc:20: undefined reference to `print_list<int, 4, 15>::print_list(int const&, int const&)'
collect2: ld returned 1 exit status

我真的不知道该怎么办。 C ++从来就不是我的一杯茶......

我的代码看起来像这样(是的,我已经注释了该程序的大部分内容)。旁注,我并不关心逻辑是否正确或类似的东西,我的主要关注点是将事物链接起来,这样我至少可以运行.exe并测试输出。

#include "/home/onyuksel/courses/340/common/340.h"

#ifndef H_PROG9
#define H_PROG9

// data files

#define D1 "/home/onyuksel/courses/340/progs/11f/p9/prog9.d1"
#define D2 "/home/onyuksel/courses/340/progs/11f/p9/prog9.d2"
#define D3 "/home/onyuksel/courses/340/progs/11f/p9/prog9.d3"

#define INT_SZ 4    // width of integer
#define FLT_SZ 7    // width of floating-pt number
#define STR_SZ 12   // width of string

#define INT_LN 15   // no of integers on single line
#define FLT_LN 9    // no of floating-pt nums on single line
#define STR_LN 5    // no of strings on single line

// function and class prototypes

// stores items from input file into vector
template <class T>
void get_list ( vector <T>&, const char* );

// construct heap from items in vector
template <class T, class P>
void construct_heap ( vector <T>&, P );

// class to compare absolute values
template <class T> class abs_less {
public:
    bool operator ( ) ( const T&, const T& ) const;
};

// structure to print items in heap, where T is data type of items,
// W is allocated size in printout, and L is max num of items printed
// on single line

template <class T, const int W, const int L>
struct print_list {
    int sz, cnt; // size of heap and counter for printing
    print_list ( const int&, const int& = 0 ); // constructor
    void operator ( ) ( const T& );
};
#endif

对于.cc文件。

#include "prog9.h"

#include "/home/onyuksel/courses/340/progs/11f/p9/prog9.h"

int main ( )
{
    vector <int>    v1;   // heap of integers
    vector <float>  v2;   // heap of floating-pt nums
    vector <string> v3;   // heap of strings

    // print header message
    cout << "\t\t\t*** CSCI 340: Program 9 - Output ***\n\n";

    // first heap

    cout << "first heap - ascending order:\n\n";
    get_list ( v1, D1 );
    construct_heap ( v1, less <int> ( ));
    print_list <int, INT_SZ, INT_LN> print1 ( v1.size ( ));
    for_each ( v1.begin ( ), v1.end ( ), print1 );

    cout << "first heap - descending order:\n\n";
    get_list ( v1, D1 );
    construct_heap ( v1, greater <int> ( ));
    for_each ( v1.begin ( ), v1.end ( ), print1 );

    cout << "first heap - ascending order with absolute values:\n\n";
    get_list ( v1, D1 );
    construct_heap ( v1, abs_less <int> ( ));
    for_each ( v1.begin ( ), v1.end ( ), print1 );

    // second heap

    cout << "second heap - ascending order:\n\n";
    get_list ( v2, D2 );
    construct_heap ( v2, less <float> ( ));
    print_list <float, FLT_SZ, FLT_LN> print2 ( v2.size ( ));
    for_each ( v2.begin ( ), v2.end ( ), print2 );

    cout << "second heap - descending order:\n\n";
    get_list ( v2, D2 );
    construct_heap ( v2, greater <float> ( ));
    for_each ( v2.begin ( ), v2.end ( ), print2 );

    cout << "second heap - ascending order with absolute values:\n\n";
    get_list ( v2, D2 );
    construct_heap ( v2, abs_less <float> ( ));
    for_each ( v2.begin ( ), v2.end ( ), print2 );

    // third heap

    cout << "third heap - ascending order:\n\n";
    get_list ( v3, D3 );
    construct_heap ( v3, less <string> ( ));
    print_list <string, STR_SZ, STR_LN> print3 ( v3.size ( ));
    for_each ( v3.begin ( ), v3.end ( ), print3 );

    cout << "third heap - descending order:\n\n";
    get_list ( v3, D3 );
    construct_heap ( v3, greater <string> ( ));
    for_each ( v3.begin ( ), v3.end ( ), print3 );

    // print termination message
    cout << "\t\t\t*** end of program execution ***\n\n";
    return 0;
}

template <class T>
void get_list ( vector <T>& v, const char* path ) {
    ifstream file;
    T data;
    v.clear()
    file.open(path);
    if(!file){
        cout << "Error opening files.\n";
        exit(1);
    }
    while(file >> data){
        v.push_back(data);
    }
    file.close();
}

template <class T, class P>
void construct_heap ( vector <T>& v, P pred ) {
    if (v.empty())
        return;
    make_heap(v.begin(), v.end());
    sort_heap(v.begin(), v.end(), pred);
}

template <class T>
bool abs_less <T> :: operator ( ) ( const T& x, const T& y ) const {
    return abs(x) < abs(y);
}

template <class T, const int W, const int L> 
void print_list <T, W, L> :: operator ( ) ( const T& x ) {
    cout << setw(W) << x;
    cnt++;
    if(cnt >= L){
        cout << "\n";
        cnt = 0;
    }
}

2 个答案:

答案 0 :(得分:2)

你给了print_list的构造函数的声明,但是你从未给出它的正文。您必须定义该函数,而不仅仅是声明它,否则您的程序将无法链接,因为链接器无法找到您尝试调用的实际函数。

使用

print_list ( const int&, const int& = 0 );

你基本上是在说“Ok编译器,将来我会编写一个与这个原型相匹配的函数,所以当我调用它时不要对我大喊大叫。”但你永远不会定义它。

答案 1 :(得分:0)

该消息似乎相当不言自明。它说你还没有定义构造函数,事实上确实如此!