0

My program has no problems until reaching the string Date::showDate member function. Why is an error occurring when no error occurs for the others. Does string have different sets of rules?

#include <string>

bool Date::setDay(int d)
{

}

bool Date::setMonth(int m)
{
    
}

bool Date::setYear(int y)
{

}

int Date::getDay()
{
    return day;
}

int Date::getMonth()
{
    return month;
}

int Date::getYear()
{
    return year;
}

string Date::showDate()
{

}
  • If you provide the actual function body, it will be easier to answer... – K.R.Park Feb 22 '22 at 01:57
  • 2
    It would also help to know what the error is. – Scott Hunter Feb 22 '22 at 01:58
  • Assuming these are actually the complete definitions: A function with non-void return type must return a value, otherwise the program will have undefined behavior when the function is called. – user17732522 Feb 22 '22 at 01:59
  • It says identifier string is undefined. I have included the header file but forgot to show that in my code, however when I defined it in the header file I got no error. – Yung Slinger Feb 22 '22 at 02:00
  • 5
    @YungSlinger It should be `std::string`, not `string`. – user17732522 Feb 22 '22 at 02:01
  • @user17732522 like this? std::string Date::showDate() { return "hello. this should show date." } – Yung Slinger Feb 22 '22 at 02:03
  • @YungSlinger Semicolon before `}` missing, but otherwise, yes. – user17732522 Feb 22 '22 at 02:04
  • Note that if you have been using `using namespace std;`, then `string` by itself also works and it is kind of confusing that it seemingly works in your header file, but not the source file implementing the functions which should be including the header file. In any case, `using namespace std;` is often considered bad practice, especially in a a header file, and it is probably better to always fully qualify the names instead. – user17732522 Feb 22 '22 at 02:05
  • I literally just added using namespace std; and it fixed my program from what I had earlier. Why is it bad practice? I am also just in general computer science for my college. @user17732522 – Yung Slinger Feb 22 '22 at 02:08
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). However, opinions on whether this is good practice in a beginner's course differ. At least if you are writing actual production code the problems mentioned are siginificant. – user17732522 Feb 22 '22 at 02:10
  • 1
    Makes a lot of sense actually. I am sure I will learn to stay away from that as we dive into deeper subjects. Thanks for the help. – Yung Slinger Feb 22 '22 at 02:15

1 Answers1

0

You don't really give us much information, I would assume you need to use std::string instead of string entirely based on the info you gave us.

dean0x45
  • 173
  • 2
  • 11