我从3周开始在大学开设C ++课程,所以我是初学者。
我有4个代码来查找错误并修复它们,其中2个已完成,但我无法弄清楚为什么没有其他2个编译。
有第一个代码;
#include <cstring>
#include <iostream>
#include <climits>
#include <cfloat>
#include <cmath>
using std::cout;
using std::endl;
template<class T>
void to_bin(T v)
{
union
{
T value;
unsigned char bytes[sizeof(T)];
};
memset(&bytes, 0, sizeof(T));
value=v;
// assumes little endian machine
for (size_t i = sizeof(T);i>0;i--)
{
unsigned char pot=128;
for (int j = 7;j>=0;j--,pot/=2)
if (bytes[i-1]&pot)
cout << "1";
else
cout << "0";
cout << " ";
}
cout << endl;
}
int main() {
cout <<" Binaerdarstellungen von -2, -1, 0, 42, INT_MAX und INT_MAX+1 als Int: " << endl;
int p_i = (-2), q_i=-1,r_i=0,s_i=42,t_i=INT_MAX,u_i=INT_MAX+1 ;
cout << "Fuer -2: ";
to_bin(p_i);
cout << endl;
cout << "Fuer -1: ";
to_bin(q_i);
cout << endl;
cout << "Fuer 0: ";
to_bin(r_i);
cout << endl;
cout << "Fuer 42: ";
to_bin(s_i);
cout << endl;
cout << "Fuer INT_MAX: ";
to_bin(t_i);
cout << "Fuer INT_MAX+1: ";
to_bin(u_i);
cout << endl;
cout << endl << "Binaerdarstellungen von -2.0, 1.0, 0.0, 42.0, FLT_MAX, FLT_MAX+1 als float: " << endl;
float p_f = -2.0, q_f= -1.0,r_f=0.0,s_f=42.0,t_f=FLT_MAX,u_f=FLT_MAX+1 ;
cout << "Fuer -2.0: ";
to_bin(p_f);
cout << endl;
cout << "Fuer -1.0: ";
to_bin(q_f);
cout << endl;
cout << "Fuer 0.0: ";
to_bin(r_f);
cout << endl;
cout << "Fuer 42.0: ";
to_bin(s_f);
cout << endl;
cout << "Fuer FLT_MAX: ";
to_bin(t_f);
cout << endl;
cout << "Fuer FLT_MAX+1: ";
to_bin(u_f);
cout << endl;
cout << endl << "Binaerdarstellungen von -2.0, 1.0, 0.0, 42.0, DBL_MAX, DBL_MAX+1 als double: " << endl;
double p_d = -2.0, q_d=-1.0,r_d=0.0,s_d=42.0,t_d=DBL_MAX,u_d=DBL_MAX+1 ;
cout << "Fuer -2.0: ";
to_bin(p_d);
cout << endl;
cout << "Fuer -1.0: ";
to_bin(q_d);
cout << endl;
cout << "Fuer 0.0: ";
to_bin(r_d);
cout << endl;
cout << "Fuer 42.0: ";
to_bin(s_d);
cout << endl;
cout << "Fuer DBL_MAX: ";
to_bin(t_d);
cout << endl;
cout << "Fuer DBL_MAX+1: ";
to_bin(u_d);
cout << endl;
cout << endl << "Groesse von Integer Variablen: ";
cout << sizeof(p_i) << endl;
cout << "Groesse von Gleitkomma Variablen mit einfacher Genauigkeit: ";
cout << sizeof(p_f) << endl;
cout << "Groesse von Gleitkomma Variablen mit doppelter Genauigkeit: ";
cout << sizeof(p_d) << endl;
return 0;}
等另一个;
#include <iostream>
#include <cstring>
using namespace std;
template<class T>
void to_bin(T v)
{
using std::cout;
using std::endl;
union
{
T value;
unsigned char bytes[sizeof(T)];
};
memset(&bytes, 0, sizeof(T));
value=v;
// assumes little endian machine
for (size_t i = sizeof(T);i>0;i--)
{
unsigned char pot=128;
for (int j = 7;j>=0;j--,pot/=2)
if (bytes[i-1]&pot)
cout << "1";
else
cout << "0";
cout << " ";
}
cout << endl;
}
int main() {
long unsigned int stack_field;
long unsigned int *heap_field = new long unsigned int;
short unsigned int *stack_pointer = (short unsigned int*)&stack_field;
short unsigned int *heap_pointer = (short unsigned int*)heap_field;
stack_field = 0;
*heap_field = 0;
*stack_pointer = 1;
*heap_pointer = 1;
stack_pointer = stack_pointer + 1;
heap_pointer = heap_pointer + 1;
to_bin(stack_field);
to_bin(*heap_field);
to_bin(*stack_pointer);
to_bin(*heap_pointer);
*stack_pointer = 1;
*heap_pointer = 1;
to_bin(stack_field);
to_bin(*heap_field);
to_bin(*stack_pointer);
to_bin(*heap_pointer);
// HIER EDITIEREN
cout << "stack_field " << sizeof(stack_field) <<" " << stack_field <<" "<< &stack_field << endl;
cout << "heap_field " << sizeof(heap_field) <<" " << *heap_field <<" "<< &heap_field << endl;
cout << "stack_pointer " << sizeof(stack_pointer) <<" " << *stack_pointer <<" "<< &stack_pointer << endl;
cout << "heap_pointer " << sizeof(heap_pointer) <<" " << *heap_pointer <<" "<< &heap_pointer << endl;
delete heap_field;
return 0;
}
我做错了什么事? 我只有3个小时才能完成并上传它。 感谢...
更新
第一个代码的错误:
x@cluster:~/x/cppfiles[534]$ g++ -Wall -Werror -o aufgabe1 aufgabe1.cpp
cc1plus: warnings being treated as errors
aufgabe1.cpp: In function 'int main()':
aufgabe1.cpp:41: error: integer overflow in expression
aufgabe1.cpp: In function 'void to_bin(T) [with T = int]':
aufgabe1.cpp:44: instantiated from here
aufgabe1.cpp:17: error: unused variable 'value'
aufgabe1.cpp:17: error: unused variable 'bytes'
aufgabe1.cpp: In function 'void to_bin(T) [with T = float]':
aufgabe1.cpp:70: instantiated from here
aufgabe1.cpp:17: error: unused variable 'value'
aufgabe1.cpp:17: error: unused variable 'bytes'
aufgabe1.cpp: In function 'void to_bin(T) [with T = double]':
aufgabe1.cpp:97: instantiated from here
aufgabe1.cpp:17: error: unused variable 'value'
aufgabe1.cpp:17: error: unused variable 'bytes'
第二个代码的错误:
x@cluster:~/x/cppfiles[534]$ g++ -Wall -Werror -o aufgabe4 aufgabe4.cpp
cc1plus: warnings being treated as errors
aufgabe4.cpp: In function 'void to_bin(T) [with T = long unsigned int]':
aufgabe4.cpp:48: instantiated from here
aufgabe4.cpp:15: error: unused variable 'value'
aufgabe4.cpp:15: error: unused variable 'bytes'
aufgabe4.cpp: In function 'void to_bin(T) [with T = short unsigned int]':
aufgabe4.cpp:50: instantiated from here
aufgabe4.cpp:15: error: unused variable 'value'
aufgabe4.cpp:15: error: unused variable 'bytes'
答案 0 :(得分:1)
你可能因为-Wall(使gcc显示“很多”警告)和-Werror(将所有警告视为错误)而得到那些eroors。
因此,如果没有给出特殊标志,你的代码可以正常编译,但在这里你强制添加警告并失败。
然后需要修复代码:
对于error: unused variable 'value'
这很简单,只需删除无用的变量即可。
对于溢出,检查值以便不溢出(修复代码);我的编译器指向int u_i=INT_MAX+1
,这显然是溢出的(你明确地将1加到最大值)
注意:添加-Wall -Werror是一个好主意(恕我直言),因为它通常指出编码错误或可以改善代码的事情。