【问题标题】:Must have pointer to object type c++ (Array)必须有指向对象类型 c++(数组)的指针
【发布时间】:2016-03-05 15:40:08
【问题描述】:

我有一个指向我创建的变量“carrierTime”的对象错误类型的指针。如果我将它设为一个数组,carrierTime 在第一个 if 语句中会变成一个错误,但是如果我没有任何数组,我会在代码的最后一行出现错误,我在乘法中使用了 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;
}

【问题讨论】:

    标签: c++ arrays pointers object


    【解决方案1】:
    cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl;
    

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

    此外,适当的调试可以帮助您自己捕捉这些错误。

    【讨论】:

    • 让我得到正确的输出carrierTime应该是carrierTime[i]..但是我得到了指针问题
    • 我不明白你的评论。
    • 您说carrierTime 使用[i++] 作为它的数组看起来不正确,但是要获得正确的输出载波时间应该有[i]。它仍然给我一个指针问题。
    • 您不能在不是数组且不是指向某物的指针的数据上使用[i][i++]。如果carrierTime 是一个数组,那么你需要重新设计你的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2015-06-11
    相关资源
    最近更新 更多