Thread: Not recognizing string type even though it is included

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Not recognizing string type even though it is included

    Hello. I am trying to currently get back into coding C++. It's been quite a while so some syntax and what not I have forgotten. I am trying to compile this code but for some reason it doesn't seem to recognize string even though it is included in the main.cpp. The error is error C2146: syntax error : missing ';' before identifier 'currentJob' Any help would be very much appreciated. Thanks

    Partition.h
    Code:
    class Partition
    {
    	public:
    		int maxSize;
    		int size;
    		int number;
    		bool status; //True = Free, False = Busy
    		int unused;
    		string currentJob;
    	
    		Partition (int,int);
    		int getMaxSize(){return maxSize;}
    		int getSize(){return size;}
    		int getNumber(){return number;}
    		int getStatus(){return status;}
    		int getUnused(){return unused;}
    		string getJob(){return currentJob;}
    
    		void setMaxSize(int m){maxSize = m;}
    		void setSize(int s){size = s;}
    		void setNumber(int n) {number = n;}
    		void setStatus(bool a)  {status = a;}
    		void setUnused(int jobSize) {maxSize - jobSize;}
    		void addJob(string jobName) {currentJob = jobName;}
    };
    
    Partition::Partition(int num, int psize)
    	{
    		number = num;
    		size = psize;
    	}
    Job.h
    Code:
    class Job
    {
    	public:
    		int currentPartitionNumber;
    		string name;
    		int size;
    		bool status; //True = Run, False = Wait
    
    		Job (string,int);
    		void setName(string n){name = n;}
    		void setSize(int s){size = s;}
    		void setStatus(bool stat){status = stat;}
    		void setPartition(int partitionNumber) {currentPartitionNumber = partitionNumber;}
    
    		string getName(){return name;}
    		int getSize(){return size;}	
    		bool getStatus(){return status;}
    		int getPartition(){return currentPartitionNumber;}
    };
    
    Job::Job(string jobName, int jsize)
    	{
    		name = jobName;
    		size = jsize;
    	}
    Main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Partition.h"
    #include "Job.h"
    using namespace std;
    
    int main ()
    {
    	int maxSize;
    	Partition pList [];
    	int partitionNumber;
    	
    	cout << "What is the total memory size?"
    	cin >> maxSize;
    
    	do
    	{
    		cout << "How many partitions are there?"
    		cin >> partitionNumber;
    
    		if(partitionNumber > 5)
    			cout << "Please enter a number smaller than 5"
    
    	}while(partitionNumber > 5);
    
    	return 0;
    }

  2. #2
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    first problem I can find is a lot of missing ;

    on the end of line of all cout use ; (semicolon)

    this is all I have found till now I'll search further

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Thanks, yea I just saw those and fixed that.I tried placing "using namespace std;" before each header and it compiled but should I really have to repeat it three times?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    I would have thought should only need to include using namespace std; once in the file?


    Otherwise use std::
    i.e.
    std::string
    std::cin
    std::cout
    etc instead of declaring namespace
    Last edited by Tropod; 10-13-2009 at 03:41 PM.

  5. #5
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    well
    using namespace std;

    means it will use somethings in <iostream>

    so if you do a class or function

    Code:
    myfunc ()
    {
              cout << "aksfjb";
    }
    using namespace std;
    you'll get compiler error

    because you haven't include std



    so you have to
    use
    using namespace std;

    after all header files

    compiler only "copies" what is included in header file in to place you have put the include

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Headers should not depend on what happens before they are included somewhere. Also headers should not pollute the global namespace with using declarations (since most likely other headers will be included afterwards which can lead to pretty much unavoidable naming conflicts with those headers, as well as causing confusion for the user file).

    This means: if a header needs std::string, then it a) includes <string>, b) refers to string as std::string.

    You can use using declarations in cpp files after all the includes, since then the namespace "pollution" only affects code in that source file.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    To make it easier, it is also possible to simply write:

    Code:
    using std::string;
    Except, if you do that for every single thing in the std:: namespace, it amounts to a ........load of stuff before your code, and looks kind of ugly, and could be considered unnecessary bloat.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM