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

标签 c++ linker

我目前正在学习 C++ 和数据结构类(class),但我终其一生都不知道如何解决我遇到的这个问题。程序可以编译,但是当我尝试链接文件以构建 .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;
    }
}

最佳答案

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

用线

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

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

关于链接文件以构建 EXE 时出现 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073577/

相关文章:

c++ - 设置不插入

C++:STL 映射,插入新值不起作用

c++ - 对 `forkpty' 的 undefined reference

c++ - 构建库时未定义对 `utf8_nextCharSafeBody_48' icu4c 的引用

c++ - 未解析的外部符号 _IID_IDXGIFactory

C++:使用 memcpy 时,myArray 和 &myArray 有什么区别?

c++ - 从大 vector 中删除第一个元素的最有效方法是什么?

c++ - 如何在 mysql++ 中使用 select 将表的值传递给 c++ 的变量

c++ - 在 VC++ 中使用单独的库进行调试和发布

c++ - Visual Studio (C++) 自动链接不需要的 lib 文件版本