So I'm working on a school project and i have a couple classes.
In one of my classes, lets call it "RedLine" im instantiating a different class called "Passangers" using the following lines
Passangers *pass; (this line is in the private section)
pass = new Passangers; (this line is in my default constructor)
it works completly fine. However, i have another class called "BlueLine" and im attempting to do literally the exact same thing since that class also uses methods from Passangers, and im doing the exact same declaration.
Passangers *pass; (in private)
pass = new Passangers; (default construrctor scetion)
Now, the compiler is throwing me an error saying nonsense like undeclared identifier, missing type specifier - int assumed, etc etc
This is extremely wierd cuz why would it work in one class but not the other? Any help would be appreciated.
Usually when the compiler says "missing type specifier", it means that you forgot to specify the type in your initialization or declaration. It assumes you want an integer, which you probably don't, and then throws you insane errors because you're using an integer in a way that's not intended to be used.
Can we see some of your code? I can point out where your error line is and guide you to fixing it.
Oh, and don't forget to use <>code<> tags when pasting code so it's easy to read.
You haven't provided "Times.h", but from the looks of it, you're trying to use a string type but the compiler can't resolve it.
That's because you're probably referring to the string from the std namespace.
Declare your strings with std::string /* name */ to resolve it.
#ifndef _Properties_h_
#define _Properties_h_
#include <fstream>
#include <iostream>
#include <map>
#include <string>
usingnamespace std;
// -----------------------------------------------------------------------------
// Properties
// ==========
// A useful object to hold all properties (from a property file, for example)
// for later access.
// Properties is implemented as a singleton so that clients don't need to
// have many (inefficient) instances.
// -----------------------------------------------------------------------------
typedef map <string, string, less< string > > kvpStore;
class Properties {
public:
// Constructor is private, as this is a singleton.
~Properties ();
// Use "getInstance()" to get a reference to the singleton.
static Properties * getInstance();
bool readPropertyFile (string fileName);
bool addProperty (string key, string value);
// Most clients just need to get a value based on a key.
string getProperty (string key);
private:
// Constructor is private, as this is a singleton.
Properties ();
// Data members
static Properties * instance;
bool readFile;
// members to support Standard Template Library map, if we choose
// to use that instead of PropertyNode
kvpStore store;
kvpStore :: iterator iterator;
};
#endif
so, my other concern is....whats the difference between RedLine and BlueLine such that RedLine works and BlueLine doesnt? It makes absolutly no sense. When i provide a forward declaration...it throws me a diferent error saying no appropriate default constructor avialable.
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(23): error C2143: syntax error : missing ';' before '*'
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(28): error C2065: 'pass' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(28): error C2061: syntax error : identifier 'Passangers'
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(64): error C2065: 'pass' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(64): error C2227: left of '->PassangersPerMinuteA' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(90): error C2065: 'pass' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\times.h(90): error C2227: left of '->PassangersPerMinuteB' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(35): error C2143: syntax error : missing ';' before '*'
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(35): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(35): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(57): error C2065: 'passanger' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(57): error C2061: syntax error : identifier 'Passangers'
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(143): error C2065: 'passanger' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(143): error C2227: left of '->boardPassangerA' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(150): error C2065: 'passanger' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(150): error C2227: left of '->disembarkPassangerA' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(154): error C2065: 'passanger' : undeclared identifier
1>c:\users\twiggy\desktop\mynewwork\code\assignmentthree\assignmentthree\blueline.h(154): error C2227: left of '->boardPassangerB' must point to class/struct/union/generic type
1> type is ''unknown-type''
No appropriate default constructor available means just that: there's no proper constructor available for the type of declaration of the object for which you specified.
You don't seam to understand what you're doing here. This is why I wish professors didn't start teaching C++ before C, it's like teaching you to run before you crawl.
But before I get started on explaining on what the problem in your code is, let me ask what's your purpose with this code? It looks to me like you're trying to make some sort of primitive linked list. Is that right?
basicly its a train simulation, it simulates 2 train lines each going from point A to point B, back and forth, picking up and disembarking passangers. Each time a train moves a station, 2 minutes pass. Every minute , people enter the train stations equally distributed based on a file the prof gave us.
I just wanna point out that this was all working before. I simply changed something (dont remember what) and suddenly its not letting me instantiate classes in those 2 particular classes. Theres no reason in my mind of why it should work completly perfectly in both classes, then suddenly stop working in 1 of them. Makes no sense.
You're using a type before it's defined.
Assuming, in your main program, you include "Passangers.h", follow through with the includes.
Before you even declare and defineclass Passangers, you include "Times.h".
One of its members, within 'class Times' is a 'Passangers *pass' type. In other words, your 'class times' is trying to use a 'class Passangers' type before the type is even declared to exist. It's complaining, so you forward declared 'class Passangers' to shut it up. However, even if you forward-declared the class, it doesn't know that a default constructor exists for that class, because that hasn't been declared yet either, which is why it's complaining that it's not available.
One of our solutions is to first fully declare class Passangers type before class Times, and to forward-declare all of the functions within those classes.
Then, in their respective .cpp files, define those functions. That way, you'll resolve all definition and declaration-related complaints.
This is some poor code, though. I don't like your proffessor, if he wrote this I'd fire him.
what kind of details makes it poor. Im still newby so ill take any advice you can give...i understand ur solution, thanks for ur help im gonna try to do it. but the more specific critisizm u give me the better it is for me, if you dont mind.
Mind, that I'm just skimming through the code and not yet looking through the logic, but just pointing out obvious things.
Firstly and probably most importantly, the rule of three is not being followed.
I see a destructor but no copy-constructor and assignment operator overloads. When you're dealing with pointers and you don't have those, things get catastrophic because if you assign an object to another of the same type, the -pointers- will be copied but not the contents as they should (meaning the destructor will run its course twice for the same block of memory).
Which of course, lead to memory leaks or worse.
Next, I see a lot of preprocessor macros. Newbies shouldn't use those until they're very comfortable with them.
I see a lot of headers, one for each class (which is okay) but you always want to properly forward-declare classes, structures, and functions before using them within another class. Follow through with your includes. If you're using a type before you declare such a thing to exist, you're shooting yourself in the foot. In cases like yours, you want to fully declare the classes and forward-declare their methods in the header, and define those methods in their respective cpp files.
Note you don't have to include the cpp files, just the headers. The compiler's linker will look for a way to resolve the function symbols by looking through all the cpp files until it finds one.
You don't have global variables, which is good. Most newbies mess that up.
As for your project, I'd start it over from scratch.
Your trains are equivalent, yes? If you're using more than one train, you should only have just one class for that and simply declare two instances of the same type.
You also want to avoid using a namespace and instead use the scope resolution operator :: to resolve namespaces (i.e. std::cout << "This is how it's done!" << std::endl; ) because using a namespace is heavy in the sense that it imports all the functions from that namespace into the global namespace, which can cause problems when functions and/or types with the same-name conflict are used. If you're going to use something like a string from namespace and nothing else, you can optionally import just that like so: using std::string;
Use a manager to manage things and store what you -need- in the classes.
To go about your project, create a managment class that manages the trains (and optionally the passangers). I don't know the exact details of your project, so I can't tell you as to how to go about recording the necessary data.
One thing I can advise though is, if both your train and your passengers depend on some sort of time, use inheritance of some kind of time class so you don't have to reinvent the necessary methods to manage time for both of them.