【发布时间】:2021-07-30 06:51:40
【问题描述】:
我正在尝试制作一个类的静态向量(称为“radsurf”),以便在构造实例时将其附加到向量中。
但我在使用 g++ 编译代码时遇到了一些问题。
它告诉我类的静态向量“没有匹配的调用函数”。
有人可以帮我吗?
树视图:
├── include
│ └── radsurf.h
└── radiacaoapp.cpp
radiacaoapp.cpp:
#include <iostream>
#include <vector>
#include "include/radsurf.h"
using namespace std;
int main(){
radsurf a(0.1); // creates one instance
radsurf b(0.5); // creates another instance
// read the list of "e" parameters of the radsurf instances
for (radsurf x : radsurf::L) {
cout << x.e << endl;
}
return 0;
}
radsurf.h:
#ifndef RADSURF_H
#define RADSURF_H
#include <vector>
class radsurf{
public:
// "e" parameter - each instance has its own
float e;
// L vector that contains all instances of radsurf classes
static std::vector<radsurf> L;
radsurf(float e){
// assign constructor argument to the "e" parameter of the instance
this->e=e;
// adds the instance to the list
L.push_back(this);
}
};
#endif // RADSURF_H
g++编译器错误:
[chandler@chandler-PC radiacaoapptest]$ g++ radiacaoapp.cpp -o test.bin
In file included from radiacaoapp.cpp:3:
include/radsurf.h: In constructor ‘radsurf::radsurf(float)’:
include/radsurf.h:15:33: error: no matching function for call to ‘std::vector<radsurf>::push_back(radsurf*)’
15 | L.push_back(this);
| ^
【问题讨论】:
-
您的向量不存储指针,因此您无法将指针推入其中。您可以推送
*this,但这会产生副本。你想用这个来完成什么? -
我需要稍后调用在执行中创建的所有实例的所有“e”参数(进行一些计算)