使用指针初始化数组,然后从中读取数据。

时间:2011-11-05 00:57:46

标签: c++ arrays pointers

这是一个用C ++编写的程序。它使用指针初始化int数组,然后读回数据。但是它代替了int值,它给出了一个垃圾字母和数字。可能是什么问题?我无法解决它。 这是cpp文件:

#include<string>
#include<cmath>
#include<sstream>
#include "TwoSeries.hpp"

using namespace std;

TwoSeries::TwoSeries() {
}

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{
    int arrayA [size];
    int arrayB [size];

    arrayA[0] = a0;
    arrayB[0] = b0;

    int *aArray = getArrayA();
    int *bArray = getArrayB();

    for(int x = 1; x < 200; x++)
    {       
        *(aArray + x) = -1;
        *(bArray + x) = -1;   

    }

}

TwoSeries::~TwoSeries() {
}

int TwoSeries::getA(int& index)
{

    return 0;
}
int TwoSeries::getB(int& index)
{

    return 0;
}

string TwoSeries::getArrays()
{
    ostringstream outString;
    string stA;
    string stB;
    string valueA;
    for(int x = 0; x < 200; x++)        
    {
        outString << x;
        string index = outString.str();

        int *arrayA = getArrayA();
        outString << *(arrayA + 1);
         valueA = outString.str();


        int * arrayB = getArrayB();
        outString << getArrayB() + x;
        string valueB = outString.str();


    //    stA += index + ":" + valueA + " ";
    //    stB += index + ":" + valueB + " ";
    }

  //  return "Series A: \n"+stA+ "/n"+"Series B: \n"+ stB;   
    return valueA;
}

int* TwoSeries::getArrayA()
{
    int * pointer = arrayA;

    return pointer;
}
int* TwoSeries::getArrayB()
{
   int * pointer = arrayB;

    return pointer;
}

这是hpp文件:

#include<string>
#include<cmath>
#include<sstream>

using namespace std;

#ifndef TWOSERIES_HPP
#define TWOSERIES_HPP

class TwoSeries {
public : 
  TwoSeries();
  TwoSeries(const int&, const int& , const int&);
  int getA(int&);
  int getB(int&);
  string getArrays();
  int* getArrayA();
  int* getArrayB();

    virtual ~TwoSeries();
private:
    int arrayA[200];
    int arrayB[200];
};

#endif  /* TWOSERIES_HPP */

这是主要方法:

#include<iostream>
#include<string>
#include "TwoSeries.hpp"

using namespace std;

/*
 * 
 */
int main() {
    const int A0 = 1; // the value at index 0 at array A
    const int B0 = 1; // the value at index 0 at array B
    const int SIZE = 200; // the size of array A and B
    const int EXIT = 4; // the digit on which the loop stops    
    int option; // stores the value entered by the user i.e. selected option from the list
    int index;  // stands for the index in either A or B array
    TwoSeries two = TwoSeries(SIZE, A0, B0);

    do
      {
        cout << "You have the following options: \n 1 - Display the current arrays\n";
        cout << " 2 - request the values of A \n 3 - request the values of B\n 4 - exit \n";
        cin >> option;
        if(option == 1)
        {
          cout << two.getArrays();  
        }
        if ((option == 2) || (option == 3))
        {
            do
            {
               cout << "Please enter the position: ";
               cin >> index;
               if((index >= 0) && (index <= (SIZE - 1)))
               {
                  if(option == 2){}
                     // getA(index);                 
                  //else
                   //   getB(index);
               }
            }
            while((index >= 0) && (index <= (SIZE - 1)));

        }
      }
      while((option != EXIT) || option < 1 || option > 4 );

    return 0;
}

2 个答案:

答案 0 :(得分:1)

这里有很多错误,所以让我们慢慢来吧:

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{
    int arrayA [size]; //no way this compiles without new [] operator
    int arrayB [size]; //also why you name identical local variable with the same name as your private data members?

    arrayA[0] = a0; //so you initialize the first member with some value
    arrayB[0] = b0; //and then all the other values to -1 is this intended?

    int *aArray = getArrayA(); //why are you doing this?
    int *bArray = getArrayB(); // you can access the data member by using this->arrayA; even without the this, but since you have identical name with locals you need the "this"

    for(int x = 1; x < 200; x++)
    {       
        *(aArray + x) = -1; //avoid pointer arithmetic, it's error prone
        *(bArray + x) = -1; //do this instead bArray[x] = -1;   

    }

}

下一步:

int TwoSeries :: getA(int&amp; index) {     //我想你想从arrayA返回一个元素?     return arrayA [index]; //容易出错,检查索引是否在边界和正数之间     返回0; }

下一步:

int* TwoSeries::getArrayA()
{
    //int * pointer = arrayA; //not needed

    return arrayA;
}
int* TwoSeries::getArrayB()
{
   //int * pointer = arrayB; //same as above

    return arrayB;
}

下一步:

string TwoSeries::getArrays()
{
    ostringstream outString;
    string stA;
    string stB;
    string valueA;
    for(int x = 0; x < 200; x++)        
    {
        outString << x;
        string index = outString.str();

        //int *arrayA = getArrayA(); //same mistake as before 
        //outString << *(arrayA + 1); //you realize you always access the same element right?
        outString << arrayA[x]; //I guess this is what you wanted?
         valueA = outString.str();


        int * arrayB = getArrayB(); // see above
        outString << getArrayB() + x;
        string valueB = outString.str();


    //    stA += index + ":" + valueA + " ";
    //    stB += index + ":" + valueB + " ";
    }

  //  return "Series A: \n"+stA+ "/n"+"Series B: \n"+ stB;   
    return valueA;
}

这里可能隐藏着更多的bug。但是你发布了一些甚至没有编译的东西,所以我们不能真的帮助你。

答案 1 :(得分:0)

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{
    int arrayA [size];
    int arrayB [size];

应该只是

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{

因为否则该函数的其余部分将假定使用局部变量而不是类&#39;私人会员。

另外,我没有看到size参数的重点,因为你在hpp文件中对数组的大小进行了硬编码?

最后一句话:

int * arrayB = getArrayB();
outString << getArrayB() + x;

应该只是

int * arrayB = getArrayB();
outString << arrayB[x];