Solved

class error

Posted on 1998-02-10
8
175 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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