为什么我的输出运算符 <<() 函数定义会导致无法解析的外部符号错误?

Why my output operator<<() function definition results in unresolved external symbol error?

本文关键字:lt 外部 错误 符号 运算符 输出 我的 为什么 定义 函数      更新时间:2023-10-16

我不知道它出了什么问题。我找不到错误在哪里。

错误为:

错误LNK2019:未解析的外部符号"类std::basic_stream>&amp__调用约定运算符&lt<(类std::basic_stream\>&,class表常量&)"(??6@YAAAV$basic_ostream@DU$char_traits@D@std@@@std@@AAV01@ABV$Table@H@@@Z)在函数_main 中引用

代码如下:

Table.h:
#include <cassert>
#include <vector>
#include <iostream>
#include <cstddef> 
template <class T>
class Table {
protected:
    std::vector<int> 
dimensions;
    // vector whose length is the number of table dimensions
    // and whose elements give the size of each dimension
    std::vector<int> 
offsetArray;
    std::vector<T> 
data;
    // vector of table values, ordered according
    // to the offsets given in offsetArray
    int
numCells;
    // total number of cells in the interior of the table
public:
    class iterator {
     public:
    iterator() : myTable(0), index(0) {}
    iterator(Table * t, int i = 0) 
        { myTable = t; index = i; };
        // construct myself to point to index i
        // of my containing Table object t
    iterator & operator++();
        // advance one element and return a reference to me
    iterator operator++(int);
        // advance one element and return my previous value
    iterator & operator=(iterator i);
        // set my table and position to be the same as j's
    bool operator==(const iterator & i);
        // return true if I'm positioned at the same element as i
    bool operator!=(const iterator & i);
        // return true if I'm positioned at a different 
        // element than i
    T & operator*();
        // return a reference to the element at my current position
    int getIndex() { return index;}
        // return my index value
private:
    Table * myTable;
    int index;
};
Table(){};
    // Construct myself to be empty
Table(const std::vector<int> & v);
    // Construct myself to be of the
    // dimensionality given by v, with elements created
    // by the default constructor
Table(const std::vector<int> & v, const T & initVal);
    // Construct myself to be of the
    // dimensionality given by v, with all cells
    // initialized to initVal
T & operator[](const std::vector<int>);
    // return a reference to the cell
    // value indexed by v
const T & operator[](const std::vector<int>) const;
    // return a const reference to the cell
    // value indexed by v
bool operator==(const Table<T> & t);
    // Return true if I'm the same dimensionality
    // as t and hold the same values
std::vector<int> size() const { return dimensions; }
    // Return my dimension vector
std::vector<int> locationFromIndex(int index) const;
    // return a location vector corresponding to 
    // an index into the (linear) data vector
int getNumCells() {return numCells};
    // return the number of cells I contain
iterator begin() {return iterator(this); };
iterator end() {return iterator(this, numCells); };
friend std::ostream & operator<<(std::ostream & out, const Table<T> & a);
};
template<class T>
std::ostream & operator<<(std::ostream & out, const Table<T> & a){
 for(size_t i=0;i<a.data.size();i++){
 out<<a.locationFromIndex(i)<<a.data[i];
 }
 return out;
 }

主.cpp文件是:

  #include <iostream>
  #include <cstdlib>
  #include "Table.h"
  using namespace std;
  void main() {
   // make this a 4x3x2 matrix, initialized with 1's
    vector<int> v;
    v.push_back(4);
    v.push_back(3);
    v.push_back(2);
   Table<int> t(v, 1);
   // print it
    cout << t << endl;
 }

我想Table.h文件中的ostream函数有问题。但我不知道怎么修。有人能帮我吗?谢谢

如果在启用警告的情况下编译,您将得到一个提示。这是来自gcc:

警告:朋友声明"std::ostream&运算符<lt;(std::ostream&,const Table&)'声明了一个非模板函数[-Wnon template friend]

注意:(如果这不是您想要的,请确保已经声明了函数模板,并在此处的函数名称后添加<>)

所以问题出在你的friend声明上:

friend std::ostream & operator<<(std::ostream & out, const Table<T> & a);

应该是:

template <typename U>
friend std::ostream & operator<<(std::ostream & out, const Table<U> & a);

一旦你解决了这个问题,你的流运营商就会出现其他问题。您正在尝试流式传输未定义函数的vector<int>。但这应该会让你走上正轨。