我尝试从办公室里找到的一本书中抽出一本天真的课。
就是这样:
#include <iostream.h>
#include <math.h>
const double ANG_RAD = 0.017;
class Angulo {
double valor;
public:
void act_valor( double );
double seno( void );
double coseno( void );
double tangente( void );
} grado;
void Angulo::act_valor( double a ) {
valor = a;
}
double Angulo::seno(void)
{
double temp;
temp = sin( ANG_RAD * this.valor );
return( temp );
}
double Angulo::coseno(void)
{
double temp;
temp = cos(ANG_RAD * valor );
return (temp);
}
double Angulo::tangente(void)
{
return ANG_RAD * valor;
}
main()
{
grado.act_valor( 60.0 );
cout << "El seno del angulo es "
<< grado.seno() << "\n";
return(0);
}
我编译了,这是我得到的错误信息(我现在明白为什么人们抱怨C ++)
In file included from /usr/include/c++/4.2.1/backward/iostream.h:31,
from Angulo.cc:1:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
Undefined symbols:
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccg0526i.o
_main in ccg0526i.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccg0526i.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccg0526i.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccg0526i.o
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccg0526i.o
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccg0526i.o
"___gxx_personality_v0", referenced from:
Angulo::act_valor(double)in ccg0526i.o
Angulo::tangente() in ccg0526i.o
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccg0526i.o
___tcf_0 in ccg0526i.o
Angulo::cosen() in ccg0526i.o
Angulo::seno() in ccg0526i.o
_main in ccg0526i.o
unsigned long const& std::min<unsigned long>(unsigned long const&, unsigned long const&)in ccg0526i.o
__static_initialization_and_destruction_0(int, int)in ccg0526i.o
global constructors keyed to gradoin ccg0526i.o
CIE in ccg0526i.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccg0526i.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(double)", referenced from:
_main in ccg0526i.o
"std::cout", referenced from:
_main in ccg0526i.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
有人可以告诉我发生了什么事吗?
我的编译器:
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~38/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
我对这本旧书中的来源进行了双重和三重检查,结果是一样的。
感谢。
答案 0 :(得分:5)
你的书已经过时了。您正在使用的编译器更加现代化,因此它只是拒绝翻译旧的非标准代码。你很可能会以某种方式将其推向工作状态,但更新代码以使其成为标准C ++会更有意义。通常,代码看起来没问题,只需要进行一些更改。
您应该使用的标头是<iostream>
,而不是<iostream.h>
。编译器已经告诉过你了。此外,标准库的组件现在位于命名空间std
中,因此main
中的输出行应如下所示
std::cout << "El seno del angulo es " << grado.seno() << "\n";
main
函数必须声明显式返回类型
int main()
{
默认情况下,C ++语言并不意味着int
。
作为额外的,不那么重要的评论,没有必要在C ++中将无参数函数声明为(void)
。仅仅()
具有完全相同的效果。虽然如果你更喜欢(void)
,它也可以。此外,无需在return
中使用()
的参数。你可以说return 0;
。看到不一致的returns
- 有些()
而有些()
不一致也很奇怪。这本书是这样的吗?
答案 1 :(得分:2)
固定来源:
#include <iostream>
#include <math.h>
const double ANG_RAD = 0.017;
class Angulo {
double valor;
public:
void act_valor( double );
double seno( void );
double coseno( void );
double tangente( void );
} grado;
void Angulo::act_valor( double a ) {
valor = a;
}
double Angulo::seno(void)
{
double temp;
temp = sin( ANG_RAD * valor );
return( temp );
}
double Angulo::coseno(void)
{
double temp;
temp = cos(ANG_RAD * valor );
return (temp);
}
double Angulo::tangente(void)
{
return ANG_RAD * valor;
}
main()
{
grado.act_valor( 60.0 );
std::cout << "El seno del angulo es "
<< grado.seno() << "\n";
return(0);
}
编译: g ++ prog.cc -o prog
答案 2 :(得分:1)
那个代码肯定有一些问题。首先,它包括<iostream.h>
,但现在它应该是<iostream>
(除非你的编译器和书一样老。)
其次,grado对象是一个静态的,没有任何理由只会让每个人感到困惑。
第三,tangente()
的代码只返回弧度的角度,而不是切线。
你想学什么?我觉得这本书不会把你带到你需要的地方。