#Compiler Error Instantizing to an Object from a Class

24 messages · Page 1 of 1 (latest)

oak hound
#

I am returning to C++ from a long hiatus programming in other languages, and I switched by compiler (g++) to use the C++11 std. I got off to a rough start do to receiving an error.

Expected Output:
The class instantizes as an object

Actual Output (error):
error: expected primary-expression before ‘myObject’

|| Obsolete

//MyClass.h
class MyClass {
  public:
  MyClass();
  virtual ~MyClass();
};

//MyClass.cpp
class MyClass {
  public:
  MyClass(void) {
    return;
  }
  ~MyClass() {
    return;
  }
}

//Program.cpp
#include "MyClass.h"
int main(int argc, char *argv[]) {
  MyClass myObject;  // <-- Error occurs here:  error: expected primary-expression before ‘myObject’
  return 0;
}

||

vast otterBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

jagged magnet
#

you are redefining the class in your implementation

#
MyClass::MyClass() {}
MyClass::~MyClass() {}
#

thats what should go in your cpp file

oak hound
# jagged magnet you are redefining the class in your implementation

Thank you! I really appreciate your response to my post. I am still getting the same error; though, thanks to your comments, I was able to fix the class cpp file.

Error:
error: expected primary-expression before ‘myObject'

//MyClass.h
class MyClass {
  public:
  MyClass();
  virtual ~MyClass();
};

//MyClass.cpp
#include "MyClass.h"
MyClass::MyClass(void) {
  return;
}
MyClass::~MyClass() {
  return;
}

//Program.cpp
#include "MyClass.h"
int main(int argc, char *argv[]) {
  MyClass myObject;  // <-- Error occurs here:  error: expected primary-expression before ‘myObject’
  return 0;
}

Any thoughts on what I might be doing wrong?

vast otterBOT
#

@oak hound Has your question been resolved? If so, type !solved :)

jagged magnet
#

there is nothing wrong with this, this compiles just fine for me

oak hound
#

Even g++ on the command line with: "$ g++ Program.cpp MyClass.cpp" gives the same error

#

I don't have to declare myObject as global, do I?

#

Well, I appreciate your help

noble violet
#

might be worthwile to show what cat Program.cpp displays to make sure you're compiling the right file as well

#

just in case you have multiple copies of program.cpp around, or if you've forgotten to save the file

#

for some more info, assuming you have the exact line MyClass myObject; and the error message points exactly to that specific line, then the issue would typically be caused by the stuff that's immediately before

#

a fairly simple example of that would be if you have a typo for semi colon and used a comma instead

#

;compile

class MyClass {};

int main() {
  int i;
  i = 0,  // <- invalid comma
  MyClass myObject;
}
oblique jettyBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:6:11: error: expected primary-expression before 'myObject'
    6 |   MyClass myObject;
      |           ^~~~~~~~
Build failed
oak hound
# noble violet for some more info, assuming you have the exact line `MyClass myObject;` and the...

https://godbolt.org/z/W3sa4W3q9 <-- That contains all of the code. I get the same error on that page. I did locate all of the code into the same pane making sure that precedence order is correct. Validated same error.

flint wolf
#

It's because your class is inside a namespace. your main is just referencing the ns, not the class

oak hound
#

That fixed it. Thank you @jagged magnet , @noble violet , and @flint wolf ! I really appreciate it.

#

!solved

vast otterBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity