|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:#include <iostream>
void convertTemperature(double tempIn, char typeIn);
void convertTemperature(int tempIn, char typeIn);
int main() {
double tempIn;
int tempInInt;
char typeIn;
std::cout << "请亲们以【xx.x C】或者 【xx.x F】格式输入一个温度:";
std::cin >> tempIn >> typeIn;
std::cin.ignore(100, '\n');
std::cout << "\n";
convertTemperature(tempIn, typeIn);
std::cout << "请亲们以【xx C】或者 【xx F】格式输入一个温度:";
std::cin >> tempInInt >> typeIn;
std::cin.ignore(100, '\n');
std::cout << "\n";
convertTemperature(tempInInt, typeIn);
return 0;
}
void convertTemperature(double tempIn, char typeIn) {
const unsigned short ADD_SUBTRACT = 32;
const double RATIO = 9.0 / 5.0;
double tempOut;
char typeOut;
switch (typeIn) {
case 'C':
case 'c':
tempOut = tempIn * RATIO + ADD_SUBTRACT;
typeOut = 'F';
typeIn = 'C';
break;
case 'F':
case 'f':
tempOut = (tempOut - ADD_SUBTRACT) / RATIO;
typeOut = 'C';
typeIn = 'F';
break;
default:
typeOut = 'E';
break;
}
if (typeOut != 'E') {
std::cout << tempIn << typeIn << '=' << tempOut << typeOut << "\n\n";
} else {
std::cout << "输入错误!"
<< "\n\n";
}
std::cout << "请输入任何字符结束程序!"
<< "\n";
std::cin.get();
}
void convertTemperature(int tempIn, char typeIn) {
const unsigned short ADD_SUBTRACT = 32;
const double RATIO = 9.0 / 5.0;
int tempOut;
char typeOut;
switch (typeIn) {
case 'C':
case 'c':
tempOut = tempIn * RATIO + ADD_SUBTRACT;
typeOut = 'F';
typeIn = 'C';
break;
case 'F':
case 'f':
tempOut = (tempOut - ADD_SUBTRACT) / RATIO;
typeOut = 'C';
typeIn = 'F';
break;
default:
typeOut = 'E';
break;
}
if (typeOut != 'E') {
std::cout << tempIn << typeIn << '=' << tempOut << typeOut << "\n\n";
} else {
std::cout << "输入错误!"
<< "\n\n";
}
std::cout << "请输入任何字符结束程序!"
<< "\n";
std::cin.get();
}
编译结果:$ gcc convertTemperature_2.cc &&./a.out
/usr/bin/ld: /tmp/ccs3MBnh.o: warning: relocation against `_ZSt4cout' in read-only section `.text'
/usr/bin/ld: /tmp/ccs3MBnh.o: in function `main':
convertTemperature_2.cc:(.text+0x24): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x2c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3a): undefined reference to `std::cin'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x42): undefined reference to `std::istream::operator>>(double&)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x54): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x65): undefined reference to `std::cin'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x6d): undefined reference to `std::istream::ignore(long, int)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x7e): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x86): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xae): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xb6): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xc4): undefined reference to `std::cin'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xcc): undefined reference to `std::istream::operator>>(int&)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xde): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xef): undefined reference to `std::cin'
/usr/bin/ld: convertTemperature_2.cc:(.text+0xf7): undefined reference to `std::istream::ignore(long, int)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x108): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x110): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: /tmp/ccs3MBnh.o: in function `convertTemperature(double, char)':
convertTemperature_2.cc:(.text+0x1fa): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x202): undefined reference to `std::ostream::operator<<(double)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x213): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x220): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x234): undefined reference to `std::ostream::operator<<(double)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x245): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x25a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x26d): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x275): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x28a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x29b): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x2a3): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x2b8): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x2bf): undefined reference to `std::cin'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x2c7): undefined reference to `std::istream::get()'
/usr/bin/ld: /tmp/ccs3MBnh.o: in function `convertTemperature(int, char)':
convertTemperature_2.cc:(.text+0x385): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x38d): undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x39e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3ab): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3bb): undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3cc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3e1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3f4): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x3fc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x411): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x422): undefined reference to `std::cout'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x42a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x43f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x446): undefined reference to `std::cin'
/usr/bin/ld: convertTemperature_2.cc:(.text+0x44e): undefined reference to `std::istream::get()'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: 错误:ld 返回 1
你这个是因为使用的是gcc编译器,gcc默认使用C语言来编译,如果要启用C++的话,要告知编译器连接stdc++库,并启用C++编译(函数重载会导致函数签名不通等问题),最简单的办法是使用gcc的一个专门编译C++的一个分支g++来编译C++
|
|