LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   GCC compiler is being difficult (https://www.linuxquestions.org/questions/linux-software-2/gcc-compiler-is-being-difficult-870801/)

coolbeans777 03-24-2011 03:28 PM

GCC compiler is being difficult
 
Hey, I'm kinda not really new to linux, but I am new to arch linux and earlier I was trying to run a simple program in c++. I proceeded to compile it with gcc like I always do
Code:

g++ main.cpp
however, it kept showing this rather obnoxious error
Code:

main.cpp:4:1: error: expected unqualified-id before ‘using’
what packages should I install through pacman to get this to work properly?

Code:

#include "CartesianCoordinate.h"
#include <iostream>

using namespace std;

int main() {
  CartesianCoordinate point(5, 6);
  int q = point.getX();
  int c = point.getY();
  cout << "Point at 'point': (" << q << " ," << c << ")" << endl;
  return 0;
}


johnsfine 03-24-2011 03:32 PM

Quote:

Originally Posted by coolbeans777 (Post 4302153)
what packages should I install through pacman to get this to work properly?

I'm pretty sure this won't be fixed by installing a package.

Post all of main.cpp if it is small. Post the first several lines if it is big.

I can't explain that error message without seeing the source code that produced it. But I'm pretty sure it represents an error in your source code.

Where did the source code come from? Maybe it uses some Microsoft specific extension to C++ that is not supported by g++

Most of the above assumes you quoted the first error message. If you skipped over some earlier error messages, the one you quoted might mean nothing at all.

corp769 03-24-2011 03:33 PM

What code are you trying to compile? By showing us, that would help out WAY more...............

johnsfine 03-24-2011 03:40 PM

How big is CartesianCoordinate.h ? Can you post the whole thing?

It now looks like the error might be in that file.

C++ is complicated enough that an incorrect construct might look like an incomplete larger construct, so the compiler can't report the error until it is working on some later line unrelated to the actual error.

#include files are processed at a level below the main compilation, so an error such as I just hypothesized often flows over from the end of an included file into the next source line in the file that included it. It may even flow across the intervening include of <iostream>

coolbeans777 03-24-2011 03:44 PM

Quote:

Originally Posted by johnsfine (Post 4302174)
How big is CartesianCoordinate.h ? Can you post the whole thing?

It now looks like the error might be in that file.

C++ is complicated enough that an incorrect construct might look like an incomplete larger construct, so the compiler can't report the error until it is working on some later line unrelated to the actual error.

Yea, sorry

CartesianCoordinate.h
[CODE]
#include <iostream>
using namespace std;

class CartesianCoordinate {
private:
int x, y;
public:
void setX(int a);
void setY(int b);
int getX();
int getY();
CartesianCoordinate(int d, int e);
}
[\CODE]

CartesianCoordinate.cpp
[CODE]
#include "CartesianCoordinate.h"
using namespace CartesianCoordinate;

CRectangle(int d, int e) {
x = d;
y = e;
}
void setX(int a) {
x = a;
}
void setY(int b) {
y = b;
}
int getX() {
return x;
}
int getY() {
return y;
}
[\CODE]

johnsfine 03-24-2011 03:45 PM

Your error was lack of a ; at the end of the class definition.

Code:

#include <iostream>
using namespace std;

class CartesianCoordinate {
private:
int x, y;
public:
void setX(int a);
void setY(int b);
int getX();
int getY();
CartesianCoordinate(int d, int e);
};


coolbeans777 03-24-2011 03:48 PM

lol didn't notice that at all. thanks

johnsfine 03-24-2011 03:53 PM

Quote:

Originally Posted by coolbeans777 (Post 4302186)
lol didn't notice that at all.

That comes with experience:

1) The code where the error was reported wasn't wrong, so an experienced programmer knows to look for an error in the preceding code.

2) The error in the preceding code needed to be the kind that couldn't be reported immediately and could flow out of the include file to the main file. Missing the ; at the end of the class definition is the most common such error, so the first thing I looked for, so I didn't need to look at any more.


All times are GMT -5. The time now is 03:20 AM.