Solved

class error

Posted on 1998-02-10
8
176 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

789 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