开发者

Cannot open output file: Building in C++

开发者 https://www.devze.com 2023-02-07 15:21 出处:网络
I am trying to learn C++, so I\'ve written an infinite-loop program in Windows that asks users to input two integer and reads out the sine and tangent values of those numbers and whether or not the fi

I am trying to learn C++, so I've written an infinite-loop program in Windows that asks users to input two integer and reads out the sine and tangent values of those numbers and whether or not the first integer is a multiple of the second.

Program1Math.h

#ifndef PROGRAM1MATH_H_
#define PROGRAM1MATH_H_
class Program1Math {
public:
  Program1Math(int, int);
  void calculateSine(int);
  void calculateTangent(int);
  void calculateModulus();
};
#endif

Program1Math.cpp

#include "Program1Math.h"
#include < iostream >
#include < math.h >
using namespace std;
int c;
int d;
Program1Math::Program1Math(int a, int b)
{ 
  c=a;
  d=b; 
}
void Program1Math::calculateSine(int a)
{ 
  cout<<"\nSine("<< a <<")\t=\t"<< sin(a); 
}
void Program1Math::calculateTangent(int a)
{ 
  cout<<"\nTan("<< a <<")\t=\t"<< tan(a); 
}
void Program1Math::calculateModulus()
{
  if (c%d==0)
  {
    cout<<"\n"<< c <<" is a multiple of "<< d <<"!"; 
  }
  else
  {
    cout<<"\n"<< c <<" is not a multiple of "<< d <<"."; 
  }
}

Program1.cpp

开发者_开发知识库
#include < iostream >
#include "Program1Math.h"
using namespace std;
int main()
{
  int num1;
  int num2;
  int i=1;

  while (i>0){
    cout<<"Please enter the first integer number:\n";
    cin>>num1;
    cout<<"Please enter the second integer number:\n";
    cin>>num2;
    Program1Math p(num1, num2);
    p.calculateModulus();
    p.calculateSine(num1);
    p.calculateTangent(num1);
    p.calculateSine(num2);
    p.calculateTangent(num2);
    cout<<"\n\n";
  }
  return 0; 
}

The program will build and run correctly (aside from a formatting issue for the calculateTangent function) in Eclipse. However, I cannot get the program to run in a Unix environment. The program will build, but when I try to run it, I get this error message:

Program1Math: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.text+0x0): first defined here
Program1Math: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o:(.fini+0x0): first defined here
Program1Math:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
Program1Math: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o:(.data+0x0): first defined here
Program1Math:(.rodata+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o:(.rodata+0x0): first defined here
Program1Math: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
Program1Math:(.dtors+0x8): first defined here
collect2: ld returned 1 exit status

Does anyone have an idea what the problem might be?


There must be a problem with your build settings. First, I would move the variables c and d into your Program1Math.h file.

Program1Math.h

#ifndef PROGRAM1MATH_H_
#define PROGRAM1MATH_H_

class Program1Math {
  private:   
    int c;
    int d;

  public:
    Program1Math(int, int);
    void calculateSine(int);
    void calculateTangent(int);
    void calculateModulus();
};
#endif

Then create a file called Makefile and add the following to it:

Makefile

all:
    g++ program1.cpp Program1Math.cpp -o Program1 -Wall

Save the Makefile in the same directory as your other files. You can now build and run this way:

> make
> ./Program1
0

精彩评论

暂无评论...
验证码 换一张
取 消