c++ - 为什么我的简单 Vector.cpp 没有链接? (从 C++ 开始)

标签 c++ vector

我直接从书中得到了 .cpp 和 .h,看起来好像没有遗漏任何东西,但我遇到了这些链接错误....

#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new>          // needed for bad__alloc exception
#include <cstdlib>      // needed for the exit function
using namespace std;

template <class T>
class simplevector
{
private:
    T *aptr;
    int arraysize;
    void memerror(); // handles mem aloc errors
    void suberror(); // handles subscripts out of range
public:
    simplevector() // default constructor
    { aptr = 0; arraysize = 0; }
    simplevector(int); // constructor
    simplevector(const simplevector &); // coppy constructor
    ~simplevector();
    int size()
        { return arraysize; }
    T &operator[](const int &); // overloaded [] operator
};

template <class T>
simplevector<T>::simplevector(int s)
{
    arraysize = s;
    // allocate memory for the array
    try
    {
        aptr = new T [s];
    }
    catch (bad_alloc)
    {
        memerror();
    }

    // initialize the array.
    for (int count = 0; count < arraysize; count++)
        *(aptr + count) = 0;
}

template <class T>
simplevector<T>::simplevector(const simplevector &obj)
{
    arraysize = obj.arraysize;
    aptr = new T [arraysize];
    if (aptr == 0)
        memerror();
    for(int count = 0; count < arraysize; count++)
        *(aptr + count) = *(obj.aptr + count);
}

template <class T>
simplevector<T>::~simplevector()
{
    if (arraysize > 0)
        delete [] aptr;
}

template <class T>
void simplevector<T>::memerror()
{
    cout << "ERROR: Cannot allocate memory.\n";
    exit(0);
}

template <class T>
T &simplevector<T>::operator [](const int &sub)
{
    if (sub < 0 || sub >= arraysize)
        suberror();
    return aptr[sub];
}

#endif

这个程序演示了 simplevector 模板:

#include <iostream>
#include "simplevector.h"
using namespace std;

int main()
{
    simplevector<int> inttable(10);
    simplevector<float> floattable(10);
    int x;

    //store values in the arrays.
    for (x = 0; x < 10; x++)
    {
        inttable[x] = (x * 2);
        floattable[x] = (x * 2.14);
    }

    //display the values in the arrays.
    cout << "these values are in inttable:\n";
    for (x = 0; x< 10; x++)
        cout << inttable[x] << " ";
    cout << endl;
    cout << "these values are in floattable:\n";
    for (x = 0; x< 10; x++)
        cout << floattable[x] << " ";
    cout << endl;

    //use the standard + operator on array elements.
    cout << "\nAdding 5 to each element of inttable"
        << " and floattable.\n";
    for (x = 0; x< 10; x++)
    {
        inttable[x] = inttable[x] + 5;
        floattable[x] = floattable[x] + 1.5;
    }

    //display the values in the array.
    cout << "These values are in inttable:\n";
    for (x = 0; x< 10; x++)
        cout << inttable[x] << " ";
    cout << endl;
    cout << "These values are in floattable:\n";
    for (x = 0; x< 10; x++)
        cout << floattable[x] << " ";
    cout << endl;
    // use the standard ++ operator on array elements.
    cout << "\nIncrementing each element of inttable and" 
        << " floattable.\n";
    for (x = 0; x< 10; x++)
    {
        inttable[x]++;
        floattable[x]++;
    }

    //display the values in the array.
    cout << "These values are in inttable:\n";
    for (x = 0; x< 10; x++)
        cout << inttable[x] << " ";
    cout << endl;
    cout << "These values are in floattable:\n";
    for (x = 0; x< 10; x++)
        cout << floattable[x] << " ";
    cout << endl;

    return 0;
}

最佳答案

它就在您在 OP 评论中发布的错误中 - 您声明了 simplevector::suberror 而没有定义它。

关于c++ - 为什么我的简单 Vector.cpp 没有链接? (从 C++ 开始),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8380987/

相关文章:

vector - 通过消耗两个向量来创建新向量的好方法是什么?

c++ - vector 修改后引用 vector.back() 的奇怪行为

c++ - 删除共享指针 vector 时可能发生内存泄漏

matlab - 在定义的圆锥区域内创建随机单位向量

c++ - 从纯 C 使用 Windows 运行时 API

c++ - 为什么 std::string.find(text,std::string:npos) 不返回 npos?

c++ - Qt 执行外部程序

c++ - 如何制作一个动态大小的数组?动态数组的一般用法(也可能是指针)?

c++ - 在 DirectX、C++ 中映射缓冲区