Question:
Problems with my c++ code?
Trenton
2011-10-11 19:31:40 UTC
This is my code: the point is to take the date and add one to it (will come later)


#include

using namespace std;
//------------------------------------------------------------------------------

class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};


Date(int y, Month m, int d);
void add_day(int n);

Month month() {return m;}
int day() {return d;}
int year() {return y;}



// ...
private:
int y;
Month m;
int d; // day
};


ostream& operator<<(ostream& os, Date& d)
{

return os << d.year() << d.month() << d.day();

}

void Date::add_day(int n) {


}

//------------------------------------------------------------------------------

int main()
try
{
Date today(1995, Date::jun, 25);


//Date dx1(Year(1998), 4, 3); // error: 2nd argument not a Month
//Date dx2(Year(1998), 4, Date::mar); // error: 2nd argument not a Month
//Date dx2(4, Date::mar, Year(1998)); // error: 1st argument not a Year
//Date dx2(Date::mar, 4, Year(1998)); // error: 2nd argument not a Month
//Date dx3(Year(1998), Date::mar, 30); // ok

//This weird and unlikely error would still not be caught until run-time:
//Date dx5(Year(4), Date::mar, 1998); // run-time error: Year::Invalid
}

catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
}

//------------------------------------------------------------------------------

right now im cant even compile this without error
the error message im getting is:


:: g++ lab6_1.cpp
Undefined first referenced
symbol in file
Date::Date(int, Date::Month, int) /var/tmp//cclhhaGI.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status



i really don't know what is wrong if someone has the answer it would be greatly appreciated

p.s. im on windows vista compiling with PuTTY
Three answers:
twostars
2011-10-11 19:48:44 UTC
Firstly, you're missing an opening brace right after "main()" here: "int main()"

Also, unless you simply didn't paste it, you are also missing a closing brace at the end of the try..catch block.



This might be causing the compiler some confusion, although on the other hand, the compiler may be letting you get away with it as it's all contained within the one statement (something I wouldn't risk, at the very least it's untidy!). Regardless, you're not returning anything unless an exception is caught. You should always return 0 to indicate success if nothing else went wrong. In this case, it'd be placed at the end of the inside of the try block (just before its closing brace).



Regarding the dates, you should be accessing them like Date::Month::mar, as it's a member of the Date class, and a member of the Month enum. Another way you might want to go about this is to simply take the enum out of the Date class - then you'd just access it as Month::mar, but that's up to you.



Good luck!
modulo_function
2011-10-11 22:46:57 UTC
The problem is with using the constructor to construct the today object.



I've never use enum. I tried all kinds of variations like

Date::Month.jun

Date::jun

Date.6

etc

But didn't get anywhere.



Then I changed the constructor signature to be (int,int,int) and it cannot find the constructor. So, I lookeed for your Date(.. ) constructor and it looks to me like you've never defined the constructor, ie, filled in the constructor body.



+add

I coded a constructor:

Date::Date(int y, Month m, int d){

y = y;

m = m;

d=d;

};



and it compiles.
edison
2016-10-02 18:10:34 UTC
a million. do no longer use void for significant use int significant() 2. attempt alongside with code with double expenses #comprise "code.h" in any different case its no looking the record for some reason probable some problem with the direction or some thing. it extremely is nice to to evaluate only alongside with iostream right now while you're in user-friendly terms making plans to place headers contained in the code header record its a greater advantageous theory in my opinion...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...