-5

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?

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
Rog Matthews
  • 3,147
  • 16
  • 37
  • 56
  • 1
    `C` doesn't have references either. – Mat Feb 28 '12 at 09:28
  • and the `init` function is not going to work like that either. – r_ahlskog Feb 28 '12 at 09:30
  • A good start is indeed to know what programming language you are using, and for which language you are compiling. If you think you are using language C, but you are actually programming in language C++, and compiling in language X, what do you think the result will be? – Lundin Feb 28 '12 at 09:33

3 Answers3

0

C and C++ are two rather different languages. Your code is neither valid C nor valid C++. It is, however, using quite a few constructs and features that are specific to C++ and are not available in C.

If you wish to learn C, my advice would be to pick up a good textbook. It'll be much more efficient than just guessing your way.

Here is a good starting point: The Definitive C Book Guide and List

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

C and C++ are two different languages. You are trying to use features of C++, which don't exist in C. I suggest to read again a C beginner guide just to clear your mind.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

there are may problems with your code : use * instead of & , using namespace is c++ code , you cant access variables in struct directly create new instance than use them.

Duke
  • 1,731
  • 2
  • 17
  • 33