简体   繁体   English

C++ 2维指针数组初始化C2440

[英]C++ 2 dimention pointer array initialization C2440

If this looks familiar, it is.如果这看起来很熟悉,那就是。 I asked a question on this last week but this time I am using pointers.上周我问了一个问题,但这次我使用了指针。 I need to input the amount of candSize to initialize the array.我需要输入 candSize 的数量来初始化数组。 If I take out the for loop, the function runs to insert a single char and a number for votes, function ends and I can print it so I know that is working as it should for now.如果我取出 for 循环,该函数会运行以插入一个字符和一个投票数字,函数结束,我可以打印它,所以我知道它现在可以正常工作。 My problem is where I try to get the 2nd dimension on the candList array.我的问题是我尝试在 candList 数组上获取第二维。 The '=' sign on the line after the for loop is getting a squiggly faulting for for 循环后行上的“=”符号出现波浪形故障

'=' : cannot convert from 'char *' to 'char' '=' : 不能从 'char *' 转换为 'char'

My text is showing this and it is the only option I have found here and other places, and they work.我的文字显示了这一点,这是我在这里和其他地方找到的唯一选择,并且它们有效。

#include<iostream>
#include<string>
#include<iomanip>
#include<array>
using namespace std;

void dataInput ( char* &candList, double* &voteList, int candSize );
//void processing ( char candName[][nameCol], double votes[][voteCol], int& index );
//void print ( char candName[][nameCol], double votes[][voteCol], int index );
int main ( )
{
    int candSize, voteSize;
    cout << "Enter number of Candidates: ";
    cin >> candSize;
    cout << endl;
    voteSize = candSize;
    char **candList = new char*[candSize];
    double **voteList = new double*[voteSize];
    for (int a = 0; a < candSize;a++)
         candList[a] = new char[8];**"Problem line"**
    for (int c = 0;c < candSize;c++)
        voteList[c] = new double[2];**"Problem line"**
    dataInput ( *candList, *voteList, candSize );

    for (int b = 0;b < candSize;b++)
    {
        cout << candList[b] << " ";
        cout << voteList[b];
        cout << endl;
    }
    system ( "Pause" );
}
void dataInput ( char *& candList, double *& voteList, int candSize )
{
    for (int a = 0; a < candSize;a++)
    {
        cout << "Enter candidate name: ";
        cin >> candList[a];
        cout << "Enter votes for candidate: ";
        cin >> voteList[a];
    }

}

Output of current code当前代码的输出

I found all my errors and found the fix.我找到了所有错误并找到了修复方法。 @Igor Tandetnik. @Igor Tandetnik。 I needed the extra * in certain spots that I would not have found.我在某些我找不到的地方需要额外的 *。 Here is the code that has fixed the problems that I had.这是修复了我遇到的问题的代码。

#include<iostream>
#include<string>
#include<iomanip>
#include<array>
using namespace std;

void dataInput ( char** &candList, double** &voteList, int candSize );
//void processing ( char candName[][nameCol], double votes[][voteCol], int& index );
//void print ( char candName[][nameCol], double votes[][voteCol], int index );
int main ( )
{
    int candSize, voteSize;
    cout << "Enter number of Candidates: ";
    cin >> candSize;
    cout << endl;
    voteSize = candSize;
    char **candList = new char*[candSize];
    double**voteList = new double*[voteSize];

    for (int a = 0; a < candSize;a++)
         candList[a] = new char[8];
    for (int c = 0;c < candSize;c++)
        voteList[c] = new double[2];
    dataInput ( candList, voteList, candSize );

    for (int b = 0;b < candSize;b++)        
    {
        cout << candList[b] << " ";
        cout << *voteList[b];
        cout << endl;
    }
    cout << endl;

}
void dataInput ( char **&candList, double **&voteList, int candSize )
{
    for (int a = 0; a < candSize;a++)   
        //for (int b = 0;b < 8;b++)
    {
        cout << "Enter candidate name: ";
        cin >> candList[a];
        cout << "Enter votes for candidate: ";
        cin >> voteList[a][0];
    }   
}

enter image description here在此处输入图片说明

Now I just need to do all the calculations needed.现在我只需要做所有需要的计算。 Hopefully since I don't need to make any more pointers I should be ok.希望因为我不需要再做任何指示,所以我应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM