我在代码块中运行代码时出错,我正在使用c和c ++

时间:2013-12-19 00:08:21

标签: c++ warnings

我的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 个答案:

答案 0 :(得分:0)

错误意味着您正在尝试链接在Mac上的Windows计算机上编译的目标文件。由于不同的硬件架构,这是不可能的。尝试删除项目中的.o文件,并从头开始重新编译。

一般来说,你的文件存在第二个问题,CodeBlocks似乎以某种方式修复但是如果你从终端编译它会导致问题:function.cpp永远不会编译,这会导致链接错误,因为链接器不会找到您在此文件中实现的功能的代码。

一个修复是在main.cpp文件中包含function.cpp,第二个用-c选项分别编译这两个文件(g ++ -c main.cpp和g ++ -c functions.cpp)并链接它们之后(g ++ main.o functions.o -o main)。

希望有所帮助。