2013-12-19 28 views
0

我的C++代碼有問題。我在代碼塊中運行我的代碼時出錯,我正在使用c和C++

這是我的代碼。

2 cpp文件(main.cpp中,function.cpp) 2頭文件(1是用於節目描述,圖1是用於圖書館和函數原型)

這四個文件是在同一文件夾用一個簡單的名稱。在該文件夾中還有一個項目文件。

我從Codeblock 12.11版本開發了一個項目,刪除了項目中默認的main.cpp。

而從代碼塊頂部的項目欄中,我添加了上面的兩個cpp文件,但不是兩個頭文件。這是我的教授的指示。

我在main.cpp中包含了兩個頭文件,並在function.cpp中包含了一個頭文件(它有庫)。

當我跑它時,它建成並運行良好。然而,當我將我的項目發送給我的教授時,他無法運行該項目。他說代碼立即存在,沒有任何反應。

在我的windows代碼塊中,IDE編譯器程序中我做了我的項目,我找不到任何錯誤。 但是,當我使用codeblock從我的mac(我有2臺計算機)嘗試相同的項目時,沒有錯誤,但是會出現如下警告。

警告:ld:警告:忽略文件obj/Debug/function.o,該文件是針對不受支持的文件格式構建的(0x4c 0x 1 0x c 0x 0 0x 0 0x 0 0x 0 0x 0 0x .... 0 0x 0)這不是被鏈接的體系結構(x86_64):obj/Debug/function.oignoring file obj ....

===構建失敗:0個錯誤。 1條警告(0分鐘,0條發送)===

我不知道在哪裏修復。

這是我的main.cpp。

#include "Project_Description.h" 
#include "Project_Header.h" 

int main() 
{ 
int score[100]; 
int highest=0; 
int sum=0; 
int n=0; 
    double id; 
    char y; 
    cout<<"Are you ready for the quiz? If yes, please type Y or y."<<endl; 
    cin>>y; 
    while(y=='y'||y=='Y') 
    { 

    score[n]=0; 
    cout<<"Enter your 8 digits student ID number: "; 
    cin>>id; 
    while(id>=100000000||id<10000000) // this while loop is to prevent wrong ID numbers. 
    { 
     cout<<"Sorry, your ID number is incorrect."<<"We need 8 digits student ID number, 
    please retype your ID number"<<endl; 
     cin>>id; 
    } 
    cout<<endl; 
    cout<<"In this quiz, each question is 2 points,so the total score is 26."<<endl; 
    cout<<"****************"<<endl; 
    score[n]=quizs(score[n]); 
//call the return function quizs 
    sum+=score[n]; 
    if (score[n]>highest) 
     highest=score[n]; 


    cout<<"Your final score is: "<<score[n]<<endl; 
    cout<<"------------------------------------------------------------------------------- 
"<<endl; 


    cout<<"Is any other student? If yes, please enter y or Y.\nIf no,enter any other  
letter."; 
    cin>>y; 
    if(y=='y'||y=='Y')//every new student should enter 'y' to enter their information. 
    { 
     cout<<"Are you ready for the quiz? If yes, please type Y or y."<<endl; 
     cin>>y; 
    } 
    n++; 

} 
cout<<"There are "<<n<<" students who took the exam!"<<endl;   
// line 48, 49, 50 are shown after the last student. 

cout <<"The average score of every students is "<< sum/n<<endl; 
cout<<"The best score is "<<highest<<endl; 
return 0;//end the program. 
} 

,這是function.cpp

#include "Project_Header.h" 


int quizs(int score) 
/*in this function, student gonna start the quizs, and this function will call function    
question1 to question13 to test students.*/ 
{ 
double answer[14];//real answer 
double stanswer[14];//student's answer 
for(int i=1; i <=14; i++)//i=question number 
{ 
    if(i==1) 
    question1(answer, stanswer);// question1 
    if(i==2) 
    question2(answer, stanswer);//question 2 
    if(i==3) 
    question3(answer, stanswer);//question 3 
    if(i==4) 
    question4(answer, stanswer);//question 4 
    if(i==5) 
    question5(answer, stanswer);//question 5 
    if(i==6) 
    question6(answer, stanswer);//question 6 
    if(i==7) 
    question7(answer, stanswer); //question 7 
    if(i==8) 
    question8(answer, stanswer); //question 8 
    if(i==9) 
    question9(answer, stanswer); //question 9 
    if(i==10) 
    question10(answer, stanswer); //question 10 

    if(i==11) 
    question11(answer, stanswer); //question 11 

    if(i==12) 
     question12(answer, stanswer); //question 12 
    if(i==13) 
     question13(answer, stanswer); //question 13 


    if (stanswer[i-1]==answer[i-1])//compare student's answer and the correct answer. 
    score=score +2;//when you get one more question right, add 2 points. 
    } 

return score;// return the final score to main function. 
} 
void question1(double *answer, double *stanswer)// questions 
{ 
cout<<"Question1:"; 
answer[0]=4; 
cout<<"1+3= "; 
cin >>stanswer[0]; 
} 
void question2(double *answer, double *stanswer) 
{ 
cout<<"Question2:"; 
answer[1]=780; 
cout<<"26*30="; 
cin>>stanswer[1]; 
} 
void question3(double *answer, double *stanswer) 
{ 
cout<<"Question3:"; 
answer[2]=4; 
cout<<"simplify "; 
cout<<"36/9="; 
cin>>stanswer[2]; 
} 
void question4(double *answer, double *stanswer) 
{ 
cout<<"Question4:"; 
answer[3]=1; 
cout<<"Find the remander of "; 
cout<<"100/9. Type your answer: "; 
cin>>stanswer[3]; 
} 
void question5(double *answer, double *stanswer) 
{ 
cout<<"Question5:"; 
answer[4]=7; 
cout<<"-4-(-11)= "; 
cin>>stanswer[4]; 
} 
void question6(double *answer, double *stanswer) 
{ 
cout<<"Question6:"; 
answer[5]=40; 
cout<<"4*(3+7)="; 
cin>>stanswer[5]; 
} 

void question7(double * answer, double *stanswer) 
{ 
cout<<"Question7:"; 
answer[6]=120; 
cout<<"What is the value of 5 factorial?"<<endl<<"5!= "; 
cin>>stanswer[6]; 
} 

void question8(double *answer, double * stanswer) 
{ 
cout<<"Question8:"; 
answer[7]=38; 
cout<<"What is the absolute value of (34-72)?"<<endl<<"|34-72|= "; 
cin>>stanswer[7]; 
} 

void question9(double *answer, double * stanswer) 
{ 
char s[99]; 
cout << "Question9:"; 
char a[6]={"2x+19"}; 
cout<<"What is derivative of (x^2+19x)?"<<endl; 
cin.ignore(256,'\n'); 
// cin.ignore works with cin.getline, cin.ignore is used to prevent errors. 
cin.getline(s,99); 
// cin the student's answer, this 's' is 'stanswer', '99' is for space. 
if(strcmp(a,s)==0) 
//strcmp(a,s)==0 means string'a' and 's' are same, strcmp(a,s)==-1 means string 'a' and  
//'s'are different. 
{ 
    answer[8]=0; 
//if the student get the correct answer, we can make answer and stanswer be same, so in  
//quiz function, the student can get the point. 
    stanswer[8]=0; 
// so these two '0's can be any numbers, but in like this way-->answer[8]=2; 
//stanswer[8]=2; 
} 

} 
void question10(double *answer, double *stanswer) 
{ 
cout << "Question10:"; 
answer[9]=10; 
cout<<"What is the square root of 100"<< endl; 
cin>>stanswer[9]; 
} 
void question11(double *answer, double *stanswer) 
{ 
char s[99]; 
cout<<"Questions 11:"; 
char a[9]={"x^5-cosx"}; 
cout<<"What is the integral of 5x^4+sinx?"<<endl; 
cin.ignore(256,'\n'); 
// cin.ignore works with cin.getline, cin.ignore is used to prevent errors. 

cin.getline(s,99); 
// cin the student's answer, this 's' is 'stanswer', '99' is for space. 

if(strcmp(a,s)==0) 
//strcmp(a,s)==0 means string'a' and 's' are same, strcmp(a,s)==-1 
//means string 'a' and 's' are different. 
{ 
    answer[10]=0; 
//if the student get the correct answer, we can make answer and 
//stanswer be same, so in quiz function, the student can get the point. 
    stanswer[10]=0; // so these two '0's can be any numbers. 
} 
} 
void question12(double *answer, double *stanswer) 
{ 
cout<<"Quetion 12:"<<endl; 
int matrix[2][2]={2,3,1,2}; // double array, matrix looks like double array 
answer[11]=1; 
cout<< "Find the det of the matrix:"<<endl<<"|"<<matrix[0][0]<<" "<<matrix[0][1]<<"| "       
<<endl<<"|"<<matrix[1][0]<<" "<<matrix[1][1]<<"|"<<endl; 
cin>>stanswer[11]; // det of the matrix = ad-bc of the matrix (a b) 
} 
//                (c d) 
void question13(double *answer, double *stanswer) 
{ 
cout<<"Question 13:"<<endl; 
char a[13]={"2x(x+8)(x-6)"}; 
cout<<"Factoring: 2x^3+4x^2-96x"<<endl; 
char s[99]; // I wrote 99 because the user might type a long answer 
cin.ignore(256, '\n'); 
// cin.ignore works with cin.getline, cin.ignore is used to prevent errors. 
cin.getline(s,99); 
// cin the student's answer, this 's' is 'stanswer', '99' is for space. 

    if(strcmp(a,s)==0) 
//strcmp(a,s)==0 means string'a' and 's' are same, strcmp(a,s)==-1 means string 'a' and  
//'s' are different. 
{ 
    answer[12]=0; 
//if the student get the correct answer, we can make answer and stanswer be same, so in 
//quiz function, the student can get the point. 

     stanswer[12]=0; // so these two '0's can be any numbers. 
    } 
} 

描述文件的名稱是Project_Description.h 它不是在該項目中添加,但在與項目相同的文件夾和兩個cpp文件。 它開始作爲

/*

和結束,因爲

*/

在側註釋有該程序的描述。 並且評論的外部沒有任何內容。

還有另一個頭文件,裏面有原型,庫。

文件名是Project_Header.h

看起來像這樣。

#include <iostream> 
#include<cstring> 


using namespace std; 
int quizs (int score); 
//in this function, student gonna start the quizs, and this function will call quation1-13     
//functions to test student 
void question1(double *answer, double *stanswer); 
void question2(double *answer, double *stanswer); 
void question3(double *answer, double *stanswer); 
void question4(double *answer, double *stanswer); 
void question5(double *answer, double *stanswer); 
void question6(double *answer, double *stanswer); 
void question7(double *answer, double *stanswer); 
void question8(double *answer, double *stanswer); 
void question9(double *answer, double *stanswer); 
void question10(double *answer, double *stanswer); 
void question11(double *answer, double *stanswer); 
void question12(double *answer, double *stanswer); 
void question13(double *answer, double *stanswer); 

這是結束。

任何人都可以幫我找到錯誤並修復它嗎?

+1

您的標題意味着您將C模塊與C++模塊混合,這是真的嗎? –

+0

在不相關的說明中,請了解結構和類。這段代碼乞求以數組方式組織。 – chrylis

回答

0

該錯誤意味着您正嘗試鏈接您的Mac上的Windows計算機上編譯的目標文件。由於硬件架構不同,這是不可能的。嘗試刪除項目中的.o文件,並從頭開始重新編譯您的Mac。

一般來說,CodeBlocks似乎會修復某些文件,但如果您從終端編譯時會導致問題,例如:function.cpp從不編譯,這會導致鏈接錯誤,因爲鏈接器會沒有找到你在這個文件中實現的功能的代碼。

一種修復方法是在main.cpp文件中包含function.cpp,另一種是使用-c選項(g ++ -c main.cpp和g ++ -c functions.cpp)分別編譯這兩個文件和鏈接他們之後(g ++ main.o functions.o -o main)。

希望有所幫助。

相關問題