C++ 模板:'Instantiation of variable required here, but no definition is available'
c++ template: 'Instantiation of variable required here, but no definition is available'
我为T
类型的矩阵创建了一个矩阵模板类。我收到一个链接器错误和一个警告,上面写着:Instantiation of variable 'ndmatrix<unsigned int>::Subscriptable::_data' required here, but no definition is available
。
模板在单独的标头中定义:
ndmatrix.h:
#ifndef ndmatrix_h
#define ndmatrix_h
#include <vector>
#include <functional>
#include <algorithm>
//Generic matrix class to hold the table levels of type T
template<class T>
class ndmatrix{
public:
size_t R; //rows
size_t C; //cols
//Hack to use [][] operator on std::vector
class Subscriptable{
public:
//Default constructor
Subscriptable(){};
Subscriptable(int idx, size_t R, size_t C) : _idx(idx), _R(R), _C(C) {
for (int i = 0; i<_C*_R; i++) {
_data.push_back(0);
}
};
Subscriptable setIdx(int i){
this->_idx = i;
return *this;
}
//This is [] operator for ndmatrix::Subscriptable
T operator[](int index) {
return _data[_C *_idx + index];
}
auto addElement(T e, int i, int j){
return _data.insert( _data.begin() + _C*i + j, e);
};
auto data() { return _data; };
private:
size_t _R; //rows
size_t _C; //cols
int _idx;
static std::vector<T> _data; <== Warning: Instantiation of variable 'ndmatrix<unsigned int>::Subscriptable::_data' required here, but no definition is available.
};
Subscriptable _s;
//This is [] operator for ndmatrix
Subscriptable operator[](int idx){
return _s.setIdx(idx);
};
//Constructors
ndmatrix();
ndmatrix(int __C, int __R);
//Member functions
void addRow(std::vector<T> row);
void print_to_console( std::function<void(T)> printer ); //printer is used to print one element of type T to the console.
};
template <class T>
ndmatrix<T>::ndmatrix(){
_s = Subscriptable();
};
template <class T>
ndmatrix<T>::ndmatrix(int __C, int __R){
_s = Subscriptable();
for (int i=0; i<__C*__R; i++) {
_s.push_back(0);
}
};
template <class T>
void ndmatrix<T>::addRow(std::vector<T> row){
this->_s.data().insert( _s.data().end() , row.begin(), row.end() );
};
template <class T>
void ndmatrix<T>::print_to_console( std::function<void(T)>printer ) {
for(int i=0; i<R; i++){
std::cout << std::endl;
for (int j=0; j<C; j++) {
printer( (*this)[i][j] );
}
}
}
#endif /* ndmatrix_h */
然后我尝试在main.cpp:
中使用它
#include <iostream>
#include <fstream>
#include <sstream>
#include "ndmatrix.h"
typedef unsigned int level_t;
//Read table of levels from file into a matrix
void read_matrix(ndmatrix<level_t>* const mat, const char* filename){
std::ifstream fileInput(filename);
int rows = 0;
while (fileInput) {
std::string line;
std::getline(fileInput, line, 'n');
std::stringstream strstream(line);
std::vector<level_t> row;
level_t level;
int cols=0;
while ( strstream >> level ) {
row.push_back(level);
cols++;
}
mat->addRow(row);
rows++;
mat->C = cols;
mat->R = rows;
//debug
std::cout << line << std::endl;
}
};
int main(int argc, const char * argv[]) {
//tests
ndmatrix<level_t> matrix;
read_matrix(&matrix, "table.dat");
// insert code here...
std::cout << "Hello, World!n";
return 0;
}
尝试构建时,我收到上面的警告和链接器错误:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
您已经编写了静态成员变量_data
的声明,但跳过了定义。
template< typename T > typename ::std::vector< T > ndmatrix< T >::Subscriptable::_data;
更新:在 C++17 中,可以使用inline
静态变量,这些变量可以在类中初始化,并且不需要在类外单独定义:
static inline std::vector<T> _data{};
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- c++20[[no.unique_address]]中的新功能是什么
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- "no matching function for call to 'Vector::Vector'"错误
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- 处理"no operator found"
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 如何解决"no Qt platform plugin could be initialized"问题?
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- do while 循环中的 if 语句以 yes 或 no 结尾
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 错误消息:使用"string* +="后"no match for 'operator+='"
- C++ 模板:'Instantiation of variable required here, but no definition is available'
- 如何修复理性狂想曲错误:"Current value is " VC11 ", but no Visual Studio installation was not found in the direc
- getaddrinfo() 返回错误"The requested name is valid, but no data of the requested type was found."
- C++ "Virtual functions but no virtual destructors"
- QScriptValue::toVariant but no fromVariant?