My instructor provided this class for us to use. He hasn't answered the e-mail I sent, and this is due soon. I keep getting an error. I haven't touched the file, just included it.

G:\ManageActivities\Date.h|23|error: 'string' does not name a type|

#ifndef DATE_H
#define DATE_H

#include <string>
#include <sstream>

class Date
{
    private:
        int m_day, m_month, m_year;

    public:

        Date(int d, int m, int y)
       { m_day = d; m_month = m; m_year = y; }

       int getDay() const     { return m_day; }

       int getMonth() const { return m_month; }

       int getYear() const     { return m_year; }

       virtual string to_string()
       {
            stringstream s;
            s << getMonth() << "/" << getDay() << "/" << getYear() ;
            return s.str();
       }
};
#endif

Recommended Answers

All 6 Replies

Namespaces. You don't have any.

std::string

string and stringstream need to be qualified with std:: or you need to put using namespace std; in your code after you includes.

Thanks guys. I have the namespace in my main calling .cpp file, is that not enough? The code above is from Date.h this ManageActivities.cpp includes it:

#include <iostream>
#include "Date.h"

using namespace std;

int main(int argc, char *argv[])
{
    Date newDate(2,14,23);
}

Do I need to actually have it in the the Date.h file? He said we weren't to touch that file. I really wish he'd reply to my e-mail. : S

If you absolutely can not not touch that file you can just place the namespace declaration prior to including the file. For example:

unsing namespace std;
#include "Date.h"

However, I think you should approach your professor with the issue at hand and show that he/she should be using namespace resolution within the header file. It is bad form to give out invalid code and expect students to live with that. Showing that you went a little beyond may get you some extra credit.

The pre-procesor sees everything like this:

#include <some file>

and replaces that with the contents of the file. So your code:

#include <iostream>
#include "Date.h"
using namespace std;
int main(int argc, char *argv[])
{
    Date newDate(2,14,23);
}

becomes this:

    #include <iostream>

    #ifndef DATE_H
    #define DATE_H
    #include <string>
    #include <sstream>
    class Date
    {
        private:
            int m_day, m_month, m_year;
        public:
            Date(int d, int m, int y)
           { m_day = d; m_month = m; m_year = y; }
           int getDay() const     { return m_day; }
           int getMonth() const { return m_month; }
           int getYear() const     { return m_year; }
           virtual string to_string()
           {
                stringstream s;
                s << getMonth() << "/" << getDay() << "/" << getYear() ;
                return s.str();
           }
    };
    #endif

        using namespace std;
        int main(int argc, char *argv[])
        {
            Date newDate(2,14,23);
        }

You need to declare a namespace you're using before you try to use it, and as you can see, in your code, you're not doing that.

So ... as @NathanOliver and @L7Sqr and other of Dani's friends have suggested ... you could use this, to provide only what you need to permit the program to recognise the items from the std namespace ... and so then to compile ok.

string and stringstream need to be qualified with std:: or you need to put using namespace std; in your code after you includes.

using namespace std; 
#include "Date.h"

Putting this all together, try using ...

using std::string;
using std::stringstream;

// then ... and only then ...

#include "Date.h"  // Note: Date.h would have been better
                     // coded with std:: before each item from
                     // std namespace
                     // to avoid you having to add
                     // the 2 lines above
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.