Solved

class error

Posted on 1998-02-10
8
172 Views
Last Modified: 2010-04-10
I'm venturing forth into VC5 and C++. I declared a class in a header file but it keeps coming back with the error:
     'class' is followed by 'CRational' (missing ','?)

It's just a textbook class declaration so I don't know why VC5 sees the " class CRational" as two identifiers. Especially when class is a blue keyword on the screen.
0
Comment
Question by:dpms
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1181715
Post your code so we can see the problem.

Is it possible that CRational is already defined as a class name?
0
 

Author Comment

by:dpms
ID: 1181716
That would be funny...
Here the code, it's just the class declaration.

#ifndef PROTOS_H
#define PROTOS_H


class CRational
{
public:
      CRational (int n = 1, int d = 2);
      void Add  (int num1, int denom1, int num2, int denom2);
      void Sub  (int num1, int denom1, int num2, int denom2);
      void Mult (int num1, int denom1, int num2, int denom2);
      void Div  (int num1, int denom1, int num2, int denom2);
      void Reduce_Form        (int a, int b);
      void Display_Fraction (int a, int b);
      void Display_As_Float (int a, int b);
private:
      int m_Numerator;
      int m_Denominator;
};

#endif
0
 
LVL 32

Expert Comment

by:jhance
ID: 1181717
There must be more to it than this.  What you have posted compiles fine:

C:\users\default>cl /c crational.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 11.00.7022 for 80x86
Copyright (C) Microsoft Corp 1984-1997. All rights reserved.

crational.cpp

C:\users\default>
0
 

Author Comment

by:dpms
ID: 1181718
Taking off the #if/defines's. I've tried compiling it as a .c file just above an empty main(). Still gives me the same thing, and that's really all there is to the code - so if it compiles for you... I don't see why it wouldn't compile for me. Wierd??
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 32

Expert Comment

by:jhance
ID: 1181719
Here's what I compiled:

//#ifndef PROTOS_H
//#define PROTOS_H


class CRational
{
public:
CRational (int n = 1, int d = 2);
void Add (int num1, int denom1, int num2, int denom2);
void Sub (int num1, int denom1, int num2, int denom2);
void Mult (int num1, int denom1, int num2, int denom2);
void Div (int num1, int denom1, int num2, int denom2);
void Reduce_Form (int a, int b);
void Display_Fraction (int a, int b);
void Display_As_Float (int a, int b);
private:
int m_Numerator;
int m_Denominator;
};

//#endif

void main()
{
}

And here's what I got:

C:\users\default>cl crational.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 11.00.7022 for 80x86
Copyright (C) Microsoft Corp 1984-1997. All rights reserved.

crational.cpp
Microsoft (R) 32-Bit Incremental Linker Version 5.10.7303
Copyright (C) Microsoft Corp 1992-1997. All rights reserved.

/out:crational.exe
crational.obj

C:\users\default>
0
 

Accepted Solution

by:
donleyp earned 50 total points
ID: 1181720
You are using a .c file extension instead of the .cpp file extension. Change the extension on your source file to .cpp and it should compile fine.
0
 

Author Comment

by:dpms
ID: 1181721
Your right. Just out of curiosity...why does it matter if it has an extension of .c or .cpp?
0
 

Expert Comment

by:donleyp
ID: 1181722
The compiler processes .c file as 'C' language by default so that existing code may be compiled without all the errors and warnings old 'C' code is prone to generate when comiled by a C++ compiler. It's basically a backward compatibility thing. One common misconception is that C is a subset of C++. This is true syntactically, but semantically it is not. Among other differences, C++ enforces much stricter typing.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now