main not recognizing my class

Sep 6, 2019 at 9:42pm
When I try to instantiate the Patient class in main using the only constructor, passing all made up values, the Patient and the first comma in the argument list have the wiggly red lines. "Identifier 'Patient' is undefined" is the message I get when I hover over the Patient object type with the cursor, and I can't figure out why, especially when I'm including the Patient.h header include statement on both, the Patient.cpp and the PatientChargesMain.cpp. (NOTE: #pragma once was automatically inserted by the system when I created the files). I am doing this program exactly as the book instructs, separating the class declaration from the function definitions even though the functions are just setters and getters. Any insights will be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include "Patient.h"
#include "Procedure.h"
using namespace std;

int main()
{
  // Testing data
  Patient patient1("Joyce", "Marie", "Sanders", "1473 Huff Place", "Somewhere",      "Tx", "30067", "123-456-7890", "Jeff Bridges", "987-456-6358");           

   return 0;

}
=================================================
This is my Patient class specification header file:
=================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma once
#include <string>
#ifndef PATIENT_H
#define PATIENT_H
using namespace std;

// Patient class declaration

class Patient
{
private:
string firstName;
string middleName;
string lastName;
string address;
string city;
string state;
string zipCode;
string phoneNumber;
string emergencyName;
string emergencyPhone;
public:
//Constructor
Patient(string, string, string, string, string, string, string, string, string, string); 
// Prototypes for Getters and Setters are here, but I'm skipping them here.
};

#endif 

=================================================
This is my Patient.ccp definition file
=================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include "Patient.h
using namespace std;

Patient::Patient(string f, string m, string l, string add, string c, string st, string zip, string ph, string emName, string emPh)
{
firstName = f;
middleName = m;
lastName = l;
address = add;
city = c;
state = st;
zipCode = zip;
phoneNumber = ph;
emergencyName = emName;
emergencyPhone = emPh;
}
//Setter and Getter definitions follow, but I skipped them here. 
Last edited on Sep 7, 2019 at 4:02am
Sep 6, 2019 at 9:55pm
> the Patient and the first comma in the argument list have the wiggly red lines.
your IDE is trying too hard to be helpful and it's screwing around.

you need to read the compiler messages, and solve the errors from top to bottom.
in Patient.h you have
using namespace;
which the compiler complains with
error: expected identifier before ‘;’ token


your intention was to write using namespace std;


also, you have some missing closing quotes, it should be obvious with syntax highlight.
Sep 6, 2019 at 9:55pm
using namespace;

It would seem, on a glance, that since this doesn't say what namespace to use, you're problem is not just at the point of instantiating a Patient in main, but of the declaration of the class itself.

Without the "std" following "namespace" in that line above (in Patient.h), I don't think the Patient class itself can compile because "string" isn't understood. It would have to be "std::string", until you fix the namespace line.

So, look at the complaints of the compile attempt for what it is saying about the Patient class to deduce the problem, fix the "namespace" line, and work from there forward.
Sep 6, 2019 at 10:05pm
Thank you both for responding so fast! Actually, I did notice the missing std and the missing quotes around "Somewhere" before I posted my message. I apologize for somehow not reflecting the changes in my post.

I just double checked, and this is what I have: Unfortunately, I'm still having the same error.

#include <string>
#include "Patient.h"
#include "Procedure.h" // I havent' even tried instantiating this class!
using namespace std;

int main()
{
// tried literals and string variables
Patient patient1("Joyce", "Marie", "Sanders", "1473 Huff Place", "Somewhere", "Tx", "30067", "123-456-7890", "Jeff Bridges", "987-456-6358");

return 0;
}
Sep 7, 2019 at 12:07am
When posting code, enclose the code inside code tags so formatting is preserved, like this:

[code]
//...your code goes here
[/code]


The art of figuring out what the compiler means (and it doesn't always say what it means) requires every little detail. Every important message, and all of the code required to build and see it.

Otherwise, we're guessing even more than you might be.

For example, I took your edited code, pasted it into my compiler (though I pasted it in one CPP file and removed the header guards and includes)....and it compiled without issue on VS 2019.


So, the code itself CAN work, the question is now - is this exactly how it is configured, and are you giving us all of the error messages the compiler generates...

I note this, too:

1
2
3
4
#pragma once
#include <string>
#ifndef PATIENT_H
#define PATIENT_H 


The "pragma once" and the include guard "PATIENT_H" are redundant. Use one or the other.

#pragma once is considered not standard, though almost every compiler does support it, and it is superior where it is supported.

Last edited on Sep 7, 2019 at 12:13am
Sep 7, 2019 at 12:35am
Hello Talavera8,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Next point DO NOT edit your original post to make changes to your code. This just confuses people who look at your code and can not find anything wrong. Then it makes the rest of the responses hard to follow.

Both of your ".cpp" files have using namespace std; in them, so this line is not needed in the header file. And to that you should read http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Also this post is recent and worth reading http://www.cplusplus.com/forum/beginner/258335/

In your header the #pragma once is not needed if you are using the include guard that you have. Also if you need to include "<string>" in the header file then something is out of sequence somewhere.

It looks like you are using some version of Visual Studio, but you did not mention that part, so it is mostly a guess.

Once I get your program loaded up I will see what else I can find.

Hope that helps,

Andy
Sep 7, 2019 at 3:49am
Thank you so much, Andy and Nicolo, for taking the time to help me out and all your input! I will definitely follow your advice. I don't know why, but the names of my files was the problem. I re-did the program with different names, and it works!
Topic archived. No new replies allowed.