必须有指向对象类型c ++(Array)的指针

时间:2015-12-01 16:24:45

标签: c++ arrays pointers object

我有一个指向变量" carrierTime"的对象错误类型的指针。我创造了。如果我使这个数组成为一个数组,那么在第一个if语句中,carrierTime会成为一个错误,但如果我在没有任何数组的情况下离开它,我会在代码的最后一行得到错误,我在乘法中使用了carrierTime。 谁能帮忙?

平台使用:视觉工作室

#include "AMcore.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string> 

using namespace std;

int main()
{
cout << "Amplitude Modulation Coursework" << endl;
cout << "Name: Mohammad Faizan Shah" << endl;
cout << "Student ID: 5526734 \n\n\n" << endl;

std::ifstream file,file2;
string filename1,filename2;
int rowCounter = 0;
double informationTime;
double informationAmplitudeAmount[361];
long double carrierTime;
double carrierAmplitudeAmount[361];
double totalAmplitudeAmount[1000];

int plotPoint;





cout << "Please enter the filename of the Carrier wave \n" << endl;
cin >> filename1;



file.open("carrier.txt");

if (file.is_open())
{



    file >> carrierTime;

    while (!file.fail())
    {
        cout << "row" << setw(3) << rowCounter;

        cout << "   Time = " << setw(5) << carrierTime;


        file >> carrierAmplitudeAmount[rowCounter];
        rowCounter++;

        if (!file.fail())
        {
            cout << "   Carrier signal= " << setw(5) << carrierAmplitudeAmount;
            file >> carrierTime;
        }
        cout << endl;
    }

    if (file.eof())
        cout << "Reached the end of file marker" << endl;
    else
        cout << "Error whilst reading input file" << endl;
}
else
{
    cout << "Error opening input file, ";
    cout << "check carrier.txt exists in the current directory." << endl;
}

file.close();


cout << "\n\n" << endl;
cout << "Please enter the filename of the information wave \n\n\n" << endl;
cin >> filename2;



file2.open("information.txt");

if (file2.is_open())
{



    file2 >> informationTime;

    while (!file2.fail())
    {
        cout << "row" << setw(3) << rowCounter;

        cout << "   Time = " << setw(5) << informationTime;


        file2 >> informationAmplitudeAmount[361];
        rowCounter++;

        if (!file2.fail())
        {
            cout << "   Carrier signal= " << setw(5) << informationAmplitudeAmount;
            file2 >> informationTime;
        }
        cout << endl;
    }

    if (file2.eof())
        cout << "Reached the end of file marker" << endl;
    else
        cout << "Error whilst reading input file" << endl;
}
else
{
    cout << "Error opening input file, ";
    cout << "check carrier.txt exists in the current directory." << endl;
}

file.close();

cout << "Reading from txt file has completed" << endl << endl;


cout << "\n\n" << endl;
cout << "\n\n" << endl;



cout << "please enter number of sample points to plot:| \n" << endl;
do{
    cin >> plotPoint;

    if (plotPoint <= 361)
    {
        cout << "\n plotting the graph.\n" << endl;
    }
    else if (plotPoint > 361)
    {
        cout << "Value is too high.. Try value lower than 361\n" << endl;

    }
} while (plotPoint > 361);

cout << "row" << setw(3) << rowCounter;
file >> carrierAmplitudeAmount[361];
rowCounter++;

plotPoint = 361 / plotPoint;

cout << "  Time   \|          Amplitude Modulation plot\n------------+--------------------------------------------------\n";
totalAmplitudeAmount[0] = carrierAmplitudeAmount[0] * informationAmplitudeAmount[0];
cout << setw(6) << carrierTime << setw(4) << "\|" << setw(48) << "*" << totalAmplitudeAmount[0] << endl;


for (int i = 1; i <= 361; i = i + plotPoint) {
    totalAmplitudeAmount[i] = informationAmplitudeAmount[i] * carrierAmplitudeAmount[i];

    int y = totalAmplitudeAmount[i] * 22;



    cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl;


}












cout << "End of program" << endl;


system("pause");

return 0;
}

1 个答案:

答案 0 :(得分:1)

cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl;

carrierTime[i++]看起来不正确。该变量未定义为指针。

此外,正确的调试可以帮助您自己捕获这些错误。