0

It's my first code with classes. the dev c++ compiler find 4 errors so i need a help. I think there's something wrong in my concept may be This was the Header file "complex.h"

class complex{
  public:
         bool ReadComplex();
  private:
          double real;
          double imag;
  };

This is the .cpp file

#include "complex.h"
#include <iostream.h>
#include <math.h>

using namespace std;

bool complex::ReadComplex()
 { cout<<"Enter the real part";
   cin>>real;
   cout<<"Enter the imaginary part";
   cin>>imag;
   return true;
                       }

and i got 4 errors

C:/Dev-Cpp/include/c++/3.4.2/mingw32/bits/c++config.h:57: error: expected unqualified-id before "namespace"

C:/Dev-Cpp/include/c++/3.4.2/mingw32/bits/c++config.h:57: error: expected ,' or;' before "namespace"

C:/Dev-Cpp/include/c++/3.4.2/mingw32/bits/c++config.h:61: error: expected namespace-name before ';' token

C:/Dev-Cpp/include/c++/3.4.2/mingw32/bits/c++config.h:61: error: `' is not a namespace


Thanks a lot,

5
  • 2
    Please add the compiler error messages to your question. Commented Apr 17, 2011 at 18:54
  • would be useful to know some of the errors at least... Commented Apr 17, 2011 at 18:55
  • 1
    You have 55 errors, and you didn't post a single one. Commented Apr 17, 2011 at 18:55
  • It makes no sense whatsoever to ask for help with errors and not even state what errors you are getting. Commented Apr 17, 2011 at 18:56
  • If you edit the code in the question, please state what you changed! Now the error messages is not for the code you have shown. Commented Apr 17, 2011 at 19:58

4 Answers 4

6

class definition should end with a ;

class complex
{
    // ....
} ;
//^ missing semi-colon
Sign up to request clarification or add additional context in comments.

Comments

2

You forgot the semicolon at the end of the class definition:

class complex{

}; //<------- here put a semicolon

Comments

2
  • Make sure you put the class in your own named namespace, or else not say using namespace std. There's a std::complex type, and although you don't include its header, implementors are allowed to include it themselves in any of the standard headers.
  • End your class definition with a semicolon: class complex { /* ... */ };
  • Don't use <iostream.h>. Use <iostream>. Things there are in the std:: namespace, by the way.
  • What's <Math.h>? Is it some 3rd party library you're using that's installed outside your project tree? If it's your own code or inside your project tree then use double quotes, not angle brackets. Double quotes ask the compiler to search for the code in your tree, whereas angle brackets ask the compiler to look in system directories.
  • Are you sure the standard math header won't do? Take a look at the <cmath> header.

You should also

  • make GetReal() and GeatImag() const functions. If the Get or Set counterparts don't do anything special you should throw them away and set the member data public. This because less code is less bugs.
  • You should take parameters as const references whenever it makes sense. Like in complex::Add(), for example, which should be a const function too if it doesn't change the object.

2 Comments

i tried math.h and it works in my compiler <cmath> and <iostream> give error without .h
@eslam everything in <iostream>, <cmath> and other standard headers is inside the std namespace. <iostream.h> is deprecated, don't use it. Similarly, you should use <cmath> and not <math.h> in C++ code. Just make sure you say std::cout and not cout. Similarly for anything else you use from these headers.
1

Your first code with classes should be:

class complex{
  };

int main()
  {
    return(0);
  }

Seriously. Get this to work, with no compiler warnings. Then add complexity a little at a time, and never add to code that doesn't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.