qq_35926316
2020-02-25 19:57
采纳率: 100%
浏览 686

请问一下,在vs2019c++里面,这段代码总是报LNK2019的错误。

报错内容如下:

错误  LNK2019 无法解析的外部符号 "private: void __thiscall myVector<int>::copy_from(int const *,int,int)" (?copy_from@?$myVector@H@@AAEXPBHHH@Z),该符号在函数 "public: __thiscall myVector<int>::myVector<int>(int const *,int)" (??0?$myVector@H@@QAE@PBHH@Z) 中被引用  ConsoleApplication1 C:\Users\qwer\source\repos\ConsoleApplication1\ConsoleApplication1.obj  1   

这个是头文件:

#include<iostream>
using namespace std;
const int DEFAULT_CAPACITY = 3;
using Rank = int;//秩
template<class T>
class increase_operation
{
public:
    void operator()(T& e)
    {
        e++;
    }
};
template<class T> class myVector
{
    friend class increase_operation<T>;
public:
//构造函数
    myVector(int default_capacity = DEFAULT_CAPACITY, int default_size = 0, T default_elem = 0)//
    {
        _capacity = default_capacity;
        _elem = new T[_capacity];
        for (_size = 0; _size < default_size; _size++)
        {
            _elem[_size] = default_elem;
        }
    }
    myVector(const T* elem, Rank n)
    {
        copy_from(elem, 0, n);
    }
    myVector(const T* elem, Rank low, Rank high)
    {
        copy_from(elem, low, high);
    }//在调用的时候的构造函数
    myVector(const myVector<T> & elem)
    {
        copy_from(elem._elem, 0, elem._size);
    }
    myVector(const myVector<T> & elem, Rank low, Rank high)
    {
        copy_from(elem._elem, low, high);
    }
//析构函数
    ~myVector()
    {
        delete[] _elem;
    }
//接口
    //重载运算符
    myVector& operator=(myVector<T> const& V)
    {
        if (_elem)
        {
            delete[] _elem;
        }
        copy_from(V._elem, 0, V._size);
    }
    T& operator[](Rank r)
    {
        return _elem[r];
    }
    //功能
    Rank insert(Rank, T const&);
    int remove(Rank, Rank);
    T remove(Rank);
    Rank find(T const&, Rank, Rank) const;
    void unsort(Rank lo, Rank hi);
    int deduplicate();

    template<class tx>
    void traverse(tx visit)
    {
        for (int i = 0; i < this->_size; i++)
        {
            visit(this->_elem[i]);
        }
    }
private:
//数据存放
    Rank _size = 0;//存放了的元素总数
    int _capacity = 0;//容器的大小
    T* _elem;//具体的元素内容
//内置实现
    void copy_from(const T* A, Rank low, Rank high);
    void expand();
    void shrink();
    void permute(myVector<T>&, Rank low = 0, Rank high = this->_size)
    {
        for (Rank i = low; i < high; i++)
        {
            swap(this->_elem[i], this->_elem[rand() % i]);
        }
    }
};

copy_from函数定义如下:

template<class T>
void myVector<T>::copy_from(const T* elem, Rank low, Rank high)
{
    this->_capacity = (high - low) << 1;
    this->_elem = new T[_capacity];
    this->_size = 0;
    while (high > low)
    {
        this->_elem[_size] = elem[low];
        _size++;
        low++;
    }
}

有大佬知道是怎么了吗?谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2020-02-25 22:20
    已采纳
    C++要求先申明,后使用
        void copy_from(const T* A, Rank low, Rank high);
    这一行要放在所有调用的前面,比如
    friend class increase_operation<T>;
    private: 
        void copy_from(const T* A, Rank low, Rank high);
    public:
    ...
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题