简体   繁体   中英

Can't Convert Single Pointer to Double Pointer

So here is my code.

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

float labweight = .1;
float quizweight = .1;
float assignmentweight = .4;
float examweight = .25;
float finalweight = .15;

void process_quiz(int &numquiz, int **quiz, int argc, char *argv[]) {
   for(int i=1; i < argc; i+=2) {
      if(argv[i][0]=='-' && argv[i][1]=='q') {
         numquiz = atoi(argv[i+1]);
         quiz = new int[numquiz];
      }
      else
     std::cout << quiz << " quizzes";
   }
   return;
}

void process_lab(int &numlab, int **lab, int argc, char *argv[]) {
   for(int i=1; i < argc; i+=2) {
      if(argv[i][0]=='-' && argv[i][1]=='q') {
         numlab = atoi(argv[i+1]);
         lab = new int[numlab];
      }
      else
     std::cout << lab << " quizzes";
   }
   return;
}

void process_assignment(int &numassignment, int **assignment, int argc, char *argv[]) {
   for(int i=1; i < argc; i+=2) {
      if(argv[i][0]=='-' && argv[i][1]=='q') {
         numassignment = atoi(argv[i+1]);
         assignment = new int[numassignment];
      }
      else
     std::cout << assignment << " assignmentzes";
   }
   return;
}

void process_exam(int &numexam, int **exam, int argc, char *argv[]) {
   for(int i=1; i < argc; i+=2) {
      if(argv[i][0]=='-' && argv[i][1]=='q') {
         numexam = atoi(argv[i+1]);
         exam = new int[numexam];
      }
      else
     std::cout << exam << " examzes";
   }
   return;
}

void process_final(int &numfinal, int **final, int argc, char *argv[]) {
   for(int i=1; i < argc; i+=2) {
      if(argv[i][0]=='-' && argv[i][1]=='q') {
         numfinal = atoi(argv[i+1]);
         final = new int[numfinal];
      }
      else
     std::cout << final << " finalzes";
   }
   return;
}

int main(int argc, char* argv[]){

    int numquiz, *quiz, numlab, *lab, numassignment, *assignment, numexam, *exam, numfinal, *final;
    if (argc == 9){
        process_quiz(numquiz, &quiz, argc, argv);
        process_lab(numlab, &lab, argc, argv);
        process_assignment(numassignment, &assignment, argc, argv);
        process_exam(numexam, &exam, argc, argv);
    }
    if (argc == 10){
        process_quiz(numquiz, &quiz, argc, argv);
        process_lab(numlab, &lab, argc, argv);
        process_assignment(numassignment, &assignment, argc, argv);
        process_exam(numexam, &exam, argc, argv);
        process_final(numfinal, &final, argc, argv);
    }
    else{
    cout << "Please input things correctly!" << endl;
    }

    cout << numlab << endl;
    cout << numquiz << endl;
    cout << numassignment << endl;
    cout << numexam << endl;
    cout << numfinal << endl;
}

So I have eliminated all other errors apart from the following:

grade.cpp: In function âvoid process_quiz(int&, int**, int, char**)â:
grade.cpp:17: error: cannot convert âint*â to âint**â in assignment
grade.cpp: In function âvoid process_lab(int&, int**, int, char**)â:
grade.cpp:29: error: cannot convert âint*â to âint**â in assignment
grade.cpp: In function âvoid process_assignment(int&, int**, int, char**)â:
grade.cpp:41: error: cannot convert âint*â to âint**â in assignment
grade.cpp: In function âvoid process_exam(int&, int**, int, char**)â:
grade.cpp:53: error: cannot convert âint*â to âint**â in assignment
grade.cpp: In function âvoid process_final(int&, int**, int, char**)â:
grade.cpp:65: error: cannot convert âint*â to âint**â in assignment

Now I have researched some about pointers but I don't understand why I can't assign the value from the array to the variable! I tried removing the extra pointer from the start of the functions, but that doesn't fix anything. I just get errors saying I can't convert from a single pointer to a double, and it says the error is where I call the functions in main.

Well, go to line 17 for example. You try to assign a pointer (return by the new operator) to a double pointer (quizz).

So you probably want to do *quizz = new ... . But you know, dereferencing a pointer without checking its value first is asking for problems later on.

But in any case, instead of fixing this, you should first learn about what a pointer is, how you use them and so on. You may get this to compile, but there are so many wrong things with what you wrote (you usually do not want to modify the arguments to a function by side effect because it is hard to maintain, etc.)

The end is not to get it to compile, but rather to get where you want to go safely. Learn concepts step by step and don't try to go too fast, that would be my advice considering your code / question.

FYI, when you get used to how pointers work, you should move to shared_ptr which give you much more safety. See how it works here .

Your issue is that you're trying to assign an int* to an int**. The new[n] operator does not return a pointer to a pointer, but rather a pointer to the start of a block of memory containing n dynamically created objects.

The solution would be to properly pass the pointers, changing the function headers to

void process_quiz(int &numquiz, int *quiz, int argc, char *argv[]) 

Note, you never call delete[] on your dynamically created data either, so you'll encounter memory leaks. Whilst I'm personally not fond of smart pointers, I suggest you read up on them for this issue, and read up on properly new'ing and delete'ing pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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