【发布时间】:2010-02-21 15:18:16
【问题描述】:
我正在学习如何使用 std::vector 并希望访问它的值和函数。我在另一个名为光谱的对象中有一个矢量对象。现在,当我尝试使用 .capacity 确定向量的容量时,如果我只声明向量,它就可以正常工作。但是当我在另一个对象中声明向量时,会出现语法错误。
错误:
test.c++:36: error: base operand of ‘->’ has non-pointer type ‘Spectrum’
正如下面已经提到的,-> 应该是一个点。
我想要的是确定容器的容量,即使它现在编译它也会给出结果 0 而不是我期望的 8。
代码:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
/* spectrum */
class Spectrum{
public:
float oct;
vector<float> band;
float total(){
int k;
float lpow;
// logarithmic summation
for(k = 0; k < oct; k++){
lpow = lpow + pow(10, band[k]);
}
return(10*log10(lpow));
}
Spectrum(int max_oct = 3){
oct = max_oct;
cout << "max oct = " << max_oct << endl;
vector<float> band(max_oct); //create vector/array with max_oct bands
cout << (int)band.capacity() << endl;
}
};
int main()
{
//does not work in a class
Spectrum input(8);
cout << (int)input->band.capacity() << endl;
//does work outside of a class
vector<float> band(8);
cout << (int)band.capacity() << endl;
}
【问题讨论】:
-
你到底需要演员做什么?这是不必要的。