1

所以这是我的代码。

#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;
}

因此,除了以下错误之外,我已经消除了所有其他错误:

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

现在我研究了一些关于指针的知识,但我不明白为什么我不能将数组中的值赋给变量!我尝试从函数的开头删除额外的指针,但这并不能解决任何问题。我只是收到错误说我无法从单个指针转换为双精度,并且它说错误是我在 main 中调用函数的地方。

4

2 回答 2

1

好吧,举个line 17例子。您尝试将指针(由new运算符返回)分配给双指针(测验)。

所以你可能想这样做*quizz = new ...。但是你知道,取消引用指针而不首先检查它的值是在稍后提出问题。

但无论如何,您应该首先了解指针是什么,如何使用它们等等,而不是解决这个问题。您可能会编译它,但是您编写的内容有很多错误(您通常不希望通过副作用修改函数的参数,因为它很难维护等)

最终不是让它编译,而是安全地到达你想去的地方。逐步学习概念,不要试图走得太快,考虑到您的代码/问题,这将是我的建议。

仅供参考,当您习惯了指针的工作方式时,您应该转向shared_ptr更安全的指针。看看它是如何工作

于 2013-03-06T03:51:12.113 回答
0

您的问题是您正在尝试将 int* 分配给 int**。new[n] 运算符不返回指向指针的指针,而是返回指向包含 n 个动态创建的对象的内存块开头的指针。

解决方案是正确传递指针,将函数头更改为

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

请注意,您也永远不会对动态创建的数据调用 delete[],因此您会遇到内存泄漏。虽然我个人不喜欢智能指针,但我建议您针对这个问题阅读它们,并阅读正确的新指针和删除指针。

于 2013-03-08T15:16:39.227 回答