小编FRi*_*idh的帖子

C++:如何为多维向量构造构造函数?

我想使用类中的构造函数创建二维和三维向量.但是,我不知道多维向量是怎样的.

一维作品:

class One{
    public:
        vector < float > myvector;

        One(int length) : myvector(length){}

};
Run Code Online (Sandbox Code Playgroud)

二维不起作用:

class Two{
    public:
        vector < vector < float > > myvector;

        Two(int length, int width) : myvector(length)(width) {}

};
Run Code Online (Sandbox Code Playgroud)

三维也不起作用:

class Three{
    public:
        vector < vector < vector < float > > > myvector;

        Three(int length, int width, int height) : myvector(length)(width)(height) {}

};
Run Code Online (Sandbox Code Playgroud)

下面的答案适用于二维向量.我希望以下代码为三维,但似乎是错误的

class Three{
    public:
        vector < vector <  vector < float > > > myvector;

        Three(int length, int width, int …
Run Code Online (Sandbox Code Playgroud)

c++ class vector multidimensional-array

2
推荐指数
1
解决办法
2494
查看次数

C++:如何访问另一个类中的类函数?

我正在学习如何使用std :: vector并希望访问其值和函数.我在另一个名为spectrum的对象中有一个矢量对象.现在,当我尝试使用.capacity确定向量的容量时,如果我只声明向量,它就可以正常工作.但是当我在另一个对象中声明向量时,我会遇到语法错误.

错误:

test.c++:36: error: base operand of ‘->’ has non-pointer type ‘Spectrum’
Run Code Online (Sandbox Code Playgroud)

如下所述, - >应该是一个点.

我想要的是确定容器的容量,即使它现在编译它给出结果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; …
Run Code Online (Sandbox Code Playgroud)

c++ class vector

0
推荐指数
1
解决办法
800
查看次数

标签 统计

c++ ×2

class ×2

vector ×2

multidimensional-array ×1