Solved

class error

Posted on 1998-02-10
8
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

690 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