I had written C++ code for a problem. After that i have tried to write a C code that can solve the same problem.
The header file Bit.h
is:
#ifndef Bit_included
#define Bit_included
struct Bit
{
int width;
int value;
};
void init();
void initvalues(int v, int w);
void initcopy(const Bit& b);
int getWidth();
int getValue();
Bit & plus(int newval);
#endif //Bit_included
The file Bit.c
is:
#include "Bit.h"
#include "math.h"
using namespace std;
void init()
{
value=0;
width=0;
}
void initvalues(int v,int w)
{
value=v;
width=w;
}
void initcopy(const Bit& b)
{
value=b.value;
width=b.width;
}
int getWidth()
{
return width;
}
int getValue()
{
return value;
}
Bit & plus(int newval)
{
value+=newval;
if(value>=pow(2,width))
cout<<"Overflow";
return *this;
}
Error in the header files are:
Line 11: error: expected ';', ',' or ')' before '&' token
Line 14: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
Error in Bit.c are:
Line 3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
In function 'init':
Line 6: error: 'value' undeclared (first use in this function)
Line 6: error: (Each undeclared identifier is reported only once
Line 6: error: for each function it appears in.)
Line 7: error: 'width' undeclared (first use in this function)
In function 'initvalues':
Line 11: error: 'value' undeclared (first use in this function)
Line 12: error: 'width' undeclared (first use in this function)
t.c: At top level:
Line 14: error: expected ';', ',' or ')' before '&' token
In function 'getWidth':
Line 21: error: 'width' undeclared (first use in this function)
In function 'getValue':
Line 25: error: 'value' undeclared (first use in this function)
t.c: At top level:
Line 27: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token
How can i write the correct code?