所以這裏是我的代碼。不能轉換單指針到雙指針
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
現在我已經研究了一些關於三分球,但我不明白爲什麼我無法從數組賦值:
#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;
}
所以我除了下列消除了所有其他錯誤到變量!我試圖從函數的開頭刪除額外的指針,但是這不能解決任何問題。我只是得到錯誤,說我不能從單個指針轉換爲雙精度,並且它說錯誤是我在main調用函數的地方。
指針是一個非常複雜的話題。這是這項任務的要求,還是你只是想使用它們?你可以做所有這些計算,只是傳遞和返回值而不使用指針。 – 2013-03-06 03:48:23