动态数组问题

时间:2014-11-28 01:10:54

标签: c++ arrays dynamic

好的我的问题是,在我的动态数组函数中,我有一个数组,它给出了下面的错误。

 #include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

class Revenue
{
};
static int Track_Num_Divisions = 1;
static int Track_Quart_Revenue = 1;
void Program_loop()
{

{

    string D;
    string DN;
    int N;
    double TS;
    double TC;
    double P;

      int arry;
    cout << "how many Revenue tiers do you want?: "; cin >> arry;
    Revenue*  rev = new Revenue[arry];//dynamic array
    for (int i = 0, Track_Num_Divisions = 1;Track_Num_Divisions, i  < arry; i++,Track_Num_Divisions++ )
    {
        Revenue& rev = rev[i];// THIS IS THE ERROR <<<<
        cout << " " << endl;
        cout << "Revenue #"<<Track_Num_Divisions << endl;
        cout << "===========" << endl;
        cout << "<< Ok what is your division name?: " << endl; cin >> D;
        string set_Division_name(D);
        cout << "<< What is your division number?: " << endl;  cin >> DN;
        string set_Division_number(DN);
        while (DN.size() != 4)
        {
            cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;


    }
    delete[] rev;
}

在函数Dynamic_array I think the problem lies in this code>> Revenue& rev =rev[i]中出现此错误:

  

错误1错误C2676:二进制'[':'收入'未定义此运算符或转换为预定义运算符可接受的类型

     

错误2 IntelliSense:没有运算符“[]”匹配这些操作数               操作数类型是:Revenue [int]

我该怎么办?

我对这个网站还是个新手还在学习正确格式的绳索。

1 个答案:

答案 0 :(得分:2)

这一行是问题所在:

Revenue& rev = *rev[i];

您取消引用rev[i]返回的值,但rev[i]不是指针或具有重载operator*的类。这是一个Revenue&

这里不需要任何东西,只需将其写成:

Revenue& rev = rev[i];