为什么我的输出运算符 <<() 函数定义会导致无法解析的外部符号错误?
Why my output operator<<() function definition results in unresolved external symbol error?
我不知道它出了什么问题。我找不到错误在哪里。
错误为:
错误LNK2019:未解析的外部符号"类std::basic_stream>&__调用约定运算符<<(类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>
。但这应该会让你走上正轨。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 在函数内部的声明中初始化数组,并在外部使用它
- 使外部项目可用于find_package CMake
- 如何防止clang格式在流运算符调用之间添加换行符<<
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- C++:来自外部文件的Trivia
- 从函数角度看ID到文件路径的内部与外部映射
- C++:将外部库链接到dll库
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- <<操作员在下面的行中工作
- 节俭并发:未解决的外部问题
- 如何在c++中从git建立外部库
- 未解析的外部符号_MsiLocateComponentW@12.
- 如何使用对C函数和类对象的外部调用来处理C++头文件
- 具有外部"c"和程序集的未定义函数
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 在C++中使用 gRPC 时未解析的外部符号
- 在类函数中初始化外部作用域变量