"Not declared in scope" C++ issue
#include "Project112.h"
Add that above main. You forgot to include the header file. And:
using namespace std;
Don't do that in a header file. That imports the everything from the std
namespace into the global namespace of any file which includes your header. Just fully qualify the type in a header, i.e., std::string
, and I would avoid that in implementation files as well in a large project (though something like using std::string
is ok IMO in an implementation file).
gmaster
Updated on June 04, 2022Comments
-
gmaster almost 2 years
I'm writing a simple class in C++ for a class (school, not code). I have a little C++ experience, but it's been a while so I'm relearning whatever I forgot and learning a lot of new syntax (I have much more experience in Java). Here is the code:
#include <iostream> #include <string> using namespace std; class Project112 { private: string romanNumeral; int decimalForm; public: Project112() { romanNumeral = ""; decimalForm = 0; } int getDecimal() { return decimalForm; } };
and here is the driver:
include cstdlib include <iostream> using namespace std; int main() { Project112 x; int value2 = x.getDecimal(); return 0; }
This is part of a larger program, but I've simplified it down to this because this is the where the problem lies. Every time I try to run the program, I get the following errors:
main.cpp:10: error: 'Project112' was not declared in this scope main.cpp:10: error: expected `;' before 'x' main.cpp:14: error: 'x' was not declared in this scope
Can someone please explain the problem? Thanks in advance.
-
chris almost 12 yearsYou don't seem to be including your class header. Good on you for reducing the code, though. One thing that you should do (Who knows, the teacher might like it better, too!) is lose the
using namespace std;
statements that pollute the global namespace.
-