2016-03-17 25 views
0

我想运行这个简单的代码。但我错过了一些东西。我试图看看操作符重载。有人能解释我在这里想念什么吗?简单的乘法或运算符问题

#include <iostream> 
#include <vector> 
#include <cstdlib> 

int main(){ 

    std::vector < std::vector<double> > tm; 
    std::vector<int> dfg; 

    // Creating a simple matrix 
    double ta1[]={0.5,0.5,0}; 
    std::vector <double> tv1 (ta1, ta1+3); 
    tm.push_back(tv1); 

    double ta2[]={0.5,0,0}; 
    std::vector <double> tv2 (ta2, ta2+3); 
    tm.push_back(tv2); 

    double ta3[]={0,0.5,0}; 
    std::vector <double> tv3 (ta3, ta3+3); 
    tm.push_back(tv3); 

    double d_load =0.5; 

    // doing some simple calculations 

    for (int destinationID = 1; destinationID <= tm.size(); destinationID++){ 
     float randomNum = ((double) rand())/((double) RAND_MAX); 
     if (randomNum <= d_load * tm[destinationID - 1]) 
      dfg.push_back(destinationID); 
    } 

    return 0; 
} 

我收到以下错误消息。

error: no match for ‘operator*’ in ‘d_load * tm.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::reference = std::vector<double>&, std::vector<_Tp, _Alloc>::size_type = long unsigned int](((long unsigned int)(destinationID + -0x00000000000000001)))’ 
+5

'tm [destinationID - 1]'是一个向量。你不能乘以一个双倍的矢量。此外,你正在比较这个产品与'浮动',这没有多大意义。 – ForceBru

+0

谢谢,我会检查它。 – user2532296

回答

1

tm元素是std::vector<std::vector<double>>,从而有效地它是一个维动态数组double。这意味着,要访问各个double元素tm,你会使用这样的:

double value = tm[destinationID - 1][theValueIndex];

这是你错过了环路的[theValueIndex]一部分。

由于我们不知道你想如何遍历数组的准确意图,所以我把它留给你来填补这个空白。

1

下面的行是无效的:

d_load * tm[destinationID - 1] 

由于tmstd::vector<std::vector<double>>,的tm的元件std::vector<double>,不double。如果你想乘每个号码或检查每个号码相匹配的情况下,你必须重复你失控的tm[]