2012-02-28 9 views
-5

問題のC++コードを書いていました。その後、私は同じ問題を解決できるCコードを書こうとしました。 ヘッダファイルBit.hは次のとおりです。C++プログラムのエラー

#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 

ファイルBit.cは次のとおりです。ヘッダファイル内

#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; 
} 

エラーは以下のとおりです。Bit.cで

Line 11: error: expected ';', ',' or ')' before '&' token 
Line 14: error: expected '=', ',', ';', 'asm' or '__attribute__' before '&' token 

エラーは以下のとおりです。

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 

正しいコードを書くにはどうすればよいですか?

+2

'using namespace std;' ?? – UmNyobe

+1

'C'には参照もありません。 – Mat

+1

CはC++ではありません。 C++はC. – pmg

答えて

0

CとC++は、かなり異なる2つの言語です。あなたのコードは有効でもなく、有効なC++でもありません。しかし、C++に固有で、C言語では利用できないいくつかの構文と機能を使用しています。

C言語を学びたい場合は、良い教科書を手に入れてください。あなたのやり方を推測するよりはるかに効率的です。ここで

は良い出発点である:The Definitive C Book Guide and List

0

CC++は、2つの異なる言語です。 C++の機能を使用しようとしていますが、これはCには存在しません。私はちょうどあなたの心をクリアするためにCの初心者ガイドを再度お読みになることをお勧めします。

0

あなたのコードを持つ可能性のある問題があります。名前空間を使用して代わりに&の 使用*は、C++コードで、構造体の中にあなたカントのアクセス変数は、それらを直接使うよりも、新しいインスタンスを作成します。

関連する問題