C++ vector 编译错误

标签 c++ vector

<分区>

源码很原始,使用斯坦福的lib vector.h

#include <iostream>
#include "vector.h"

void countIntRange(Vector<int>& v, int a, int b);

int main(){
    Vector <int> v;
    v.add(28);
    v.add(1);
    v.add(17);
    v.add(4);
    v.add(41);
    v.add(9);
    v.add(59);
    v.add(8);
    v.add(31);
    v.add(30);
    v.add(25);
    int min = 10;
    int max = 30;
    countIntRange(v, min, max);

    return 0;
}

void countIntRange(Vector<int>& v, int a, int b){
    int count = 0;
    for (int i = 0; i < v.size(); i++){
        if (v.get(i) > a && v.get(i) < b){
            count++;
        }
    }
    cout << count;
}

在编译 g++ 时抛出这样的错误:

highlander@linux-f62d:~/Documents/CS106b/libs> g++ ex31.cpp
/tmp/ccfH1i0N.o: In function `Vector<int>::get(int) const':
ex31.cpp:(.text._ZNK6VectorIiE3getEi[_ZNK6VectorIiE3getEi]+0x4b): 
    undefined reference to `error(std::string)'
/tmp/ccfH1i0N.o: In function `Vector<int>::insert(int, int)':
ex31.cpp:(.text._ZN6VectorIiE6insertEii[_ZN6VectorIiE6insertEii]+0x6c): 
    undefined reference to `error(std::string)'
collect2: error: ld returned 1 exit status

这是错误信息.... 它不喜欢什么?

这是整个 vector.h 代码 https://gist.github.com/HighlanderGe/9023734

最佳答案

解决它的方法取决于您如何使用 Stanford cslib library :

  • 如果你把它编译成一个库,比如说StanfordCSLib,你需要添加-lStanfordCS到你的g++命令行
  • 如果您想直接使用库源代码,您需要将StanfordCS/*.cpp 添加到您给g++ 的文件列表中。

第一种方法更好,因为它避免了多次重新编译。如果您需要让一组源工作,则第二种方法可能更方便。

关于C++ vector 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802117/

相关文章:

c++ - 缺少模板参数

c++ - C++ 中访问器函数的段错误

c++ - 是否可以使用非常量键类型的 unordered_map?

c++ - gcc4.9.2的std::vector的libstdc++实现继承自_Vector_base(非虚拟析构函数)。为什么这样可以?

c++ - 如何使用If和else语句在 vector 中搜索

c++ - 如何使用 std::vector<std::tuple<A,B>> 来管理内存(调整大小、保留...),但实际上保持 B 之前的 As 连续

c++11 - C++ STL;迭代包含STL容器的类?

c++ - VS 2015 宏解释

c++ - 抽象类 C++ 的 VTABLE

arrays - Solana Rust 智能合约如何处理数组和向量?