Is it possible to call cpp file into main, without using header?
I tried in VS 2019 something like that:
main.cpp
#include<iostream>
int main()
{
Date birthday{3,2,2};
birthday.getDay();
std::cout << "User, input the day";
int u_day = 0;
std::cin >> u_day;
birthday.print(u_day);
return 0;
}
another.cpp
#include<iostream>
class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
Date() = default;
Date(int year, int month, int day) {
setDate(year, month, day);
}
void setDate(int year, int month, int day) {
m_year = year;
m_month = month;
m_day = day;
}
int getYear() { return m_year; }
int getMonth() { return m_month; }
int getDay() { return m_day; }
void print(int day) {
m_day = day;
std::cout << m_day;
}
};
Now there are a lot of C2056 errors,
When I add Date::Date birthday{3,2,2}; in the main.cpp
I get E0276, E0065, C2653,C2065,C2146
Related
In main(), I am making an array myAppointments, and realize to make it work I would need to create a default constructor in Appointments.h, but when I do I get this error:
no matching function for call to 'Time::Time()'
Appointment(){
Here is Main:
/*
* Homework 4 -- UPDATE as needed
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "Appointment.h"
using namespace std;
void callPrint (Time &TimeOrApptObject) { TimeOrApptObject.print(); }
int main(){
int month, day, year, hour, minute,howLong;
Appointment myAppointments[19];
ifstream HW4DataFileHandle;
HW4DataFileHandle.open("Lab6Data.txt");
while (!HW4DataFileHandle.eof( )) {
for (int i = 1; i < 20; i++) {
HW4DataFileHandle>>month;
HW4DataFileHandle>>day;
HW4DataFileHandle>>year;
HW4DataFileHandle>>hour;
HW4DataFileHandle>>minute;
HW4DataFileHandle>>howLong;
myAppointments[i] = Appointment( month, day, year, hour, minute, howLong);
}
cout <<"enter a month" <<endl;
cin >> month;
cout <<"enter a day" <<endl;
cin >> day;
cout <<"enter a year"<<endl;
cin >> year;
Date myDate( month, day, year);
cout <<"Appointments for" << month <<"/" << day <<"/" << year <<":"<< endl;
for (int i = 0; i <13; i++){
if ( myAppointments[i]==Date myDate )
{
Time thisTime = myAppointments[i];
thisDate.print();
cout << endl;
}
}
}
And here are the header files:
Date.H
// Date.h -- Class Date UPDATE as needed
#ifndef DATE_H
#define DATE_H
class Date {
private:
int month;
int day;
int year;
public:
Date(int m, int d, int y) : month(m), day(d), year(y)
{
}
friend bool friendTorCompare2Dates (const Date&,const Date& );
};
bool friendTorCompare2Dates (const Date& Right, const Date& Left)
{
if (Right.month == Left.month && Right.day == Left.day )
return true;
else
return false;
}
#endif
Time.H
//Time.h -- Class Time UPDATE as needed
using namespace std;
#include<iostream>
#ifndef TIME_H
#define TIME_H
class Time {
private :
int hour; int minute;
public:
Time(int h, int m) : hour(h)
{
}
virtual void print() {
cout << hour << " " << minute <<" " ;
}
};
#endif
Appointment.h
// Appointment.h -- Class Appointment UPDATE as needed
//
using namespace std;
#include "Time.h"
#include "Date.h"
#ifndef APPOINTMENT_H
#define APPOINTMENT_H
class Appointment: public Date, public Time {
private:
int howLong;
public:
Appointment(int month, int day, int year, int hour, int minute, int howLong) :
Date(month, day, year), Time(hour, minute), howLong(howLong)
{
}
Appointment(){
month;
day;
year;
hour;
minute;
howLong;
}
};
#endif
What do I need to change in order to have my default constructor in Appointment work? If you notice any thing else, or have any other questions in regards to my question, please let me know. I would appreciate it if you would include an example in your answer.
This line:
Appointment myAppointments[19];
will call the default constructor of Appointment for each element.
Your default constructor doesn't explicitly call a constructor of Time, so the default constructor of Time will be called, which doesn't exist. Same with Date.
You can reinstate the default constructor like this:
class Time {
// ...
public:
Time() = default;
};
To get reasonable default behavior, you should provide default initial values for the members of your class, e.g.
class Time {
private :
int hour = 0;
int minute = 0;
public:
// ...
};
When you declare the array Appointment myAppointments[19];, the default constructor for Appointment is used immediately for all 19 elements. However, Date and Time do not have default constructors defined, but Appointment's default constructor will try to call them both implicitly, hence the error you are seeing since they don't exist.
So, you have 4 different ways to fix this:
make Appointment's default constructor call Date's and Time's non-default constructors explicitly (just like your other Appointment constructor does), passing in default values to them:
Appointment() :
Date(0, 0, 0), Time(0, 0), howLong(0)
{
}
give Date and Time their own default constructors, eg:
class Date {
private:
int month;
int day;
int year;
public:
Date() : month(0), day(0), year(0)
{
}
Date(int m, int d, int y) : month(m), day(d), year(y)
{
}
...
};
class Time {
private :
int hour;
int minute;
public:
Time() : hour(0), minute(0)
{
}
Time(int h, int m) : hour(h), minute(m)
{
}
...
};
Alternatively:
class Date {
private:
int month;
int day;
int year;
public:
Date(int m = 0, int d = 0, int y = 0) : month(m), day(d), year(y)
{
}
...
};
class Time {
private :
int hour;
int minute;
public:
Time(int h = 0, int m = 0) : hour(h), minute(m)
{
}
...
};
Alternatively (C++11 and later only):
class Date {
private:
int month = 0;
int day = 0;
int year = 0;
public:
Date() = default;
// or:
// Date() : Date(0, 0, 0) {}
Date(int m, int d, int y) : month(m), day(d), year(y)
{
}
...
};
class Time {
private :
int hour = 0;
int minute = 0;
public:
Time() = default;
// or:
// Time() : Time(0, 0) {}
Time(int h, int m) : hour(h), minute(m)
{
}
...
};
construct the array elements explicitly using placement-new so you can use Appointment's non-default constructor on them, eg:
typedef unsigned char AppointmentBuf[sizeof(Appointment)];
// better:
// #include <type_traits>
// using AppointmentBuf = std::aligned_storage<sizeof(Appointment), alignof(Appointment)>::type;
int main(){
AppointmentBuf myAppointments[19];
...
for (int i = 0; i < 19; i++) {
...
new (&myAppointments[i]) Appointment(month, day, year, hour, minute, howLong);
}
...
for (int i = 0; i < 19; i++) {
Appointment &appt = reinterpret_cast<Appointment&>(myAppointments[i]);
// use appt as needed...
}
...
// cleanup
for (int i = 0; i < 19; i++) {
reinterpret_cast<Appointment&>(myAppointments[i]).~Appointment();
}
return 0;
}
Change the array to hold Appointment* pointers, and then construct the array elements dynamically via new using Appointment's non-default constructor, eg:
int main(){
Appointment* myAppointments[19] = {};
...
for (int i = 0; i < 19; i++) {
...
myAppointments[i] = new Appointment(month, day, year, hour, minute, howLong);
}
...
for (int i = 0; i < 19; i++) {
// use myAppointments[i] as needed...
}
...
// cleanup
for (int i = 0; i < 19; i++) {
delete myAppointments[i];
}
return 0;
}
Alternatively (C++11 or later only):
#include <memory>
int main(){
std::unique_ptr<Appointment> myAppointments[19];
...
for (int i = 0; i < 19; i++) {
...
myAppointments[i] = std::make_unique<Appointment>(month, day, year, hour, minute, howLong);
}
...
for (int i = 0; i < 19; i++) {
// use myAppointments[i] as needed...
}
...
// no manual cleanup needed
return 0;
}
So my challenge is: create a function that receives all of the parameters from the other get functions, like:
int get_day() const {return day;}
int get_month() const {return month;}
int get_year() const {return year;}
I want get_date() that receives all of the above gets. How can I do that? For example:
int get_date() const {
get_day();
get_month();
get_year();
}
You may pack the date like this:
int get_date() const {
return get_day() + 100 * get_month() + 10000 * get_year();
}
Note, that this is just integer, that looks like date. If you print today's date, it will be the number 20190423, which is just the number 20,190,423.
As I mentioned in the comments, it makes no sense to return a single int when you need your function to return 3 ints.
Just return an array like this:
#include <iostream>
#include <array>
struct Cal
{
typedef std::array<int,3> Date;
int day = 13;
int month = 7;
int year = 2019;
int getDay() const
{
return day;
}
int getMonth() const
{
return month;
}
int getYear() const
{
return year;
}
Date getDate() const
{
return {{getDay(),getMonth(),getYear()}};
}
};
int main()
{
Cal c;
for (auto &&i : c.getDate())
std::cout<< i <<" ";
std::cout<<std::endl;
}
The code outputs:
13 7 2019
Also, it's best if you simply returned the actual members instead of calling the getter functions. Besides the getDate() function is also a member of the class.
Date getDate() const
{
return {{day,month,year}};
}
Online code example: https://rextester.com/WPXX24681
you can use tm struct as in here: std::tm
std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);
std::cout << (now->tm_year + 1900) << (now->tm_mon + 1) << (now->tm_mday);
don't reinvent the wheel
I keep getting an compiler error with my program in Visual Studio Express 13
I commented the 2 lines in my code where the compiler error is showing up
Date.cpp
#include "Date.h"
using namespace std;
Date::Date(int d, int m, int y) : day(d), month(m), year(y)
{}
Date::Date() : day(0), month(0), year(0)
{}
const int Date::getDay() { return day; }
const int Date::getMonth() { return month; }
const int Date::getYear(){ return year; }
bool Date::operator<(const Date dOther)
{
return (year < dOther.year) || (year == dOther.year && month < dOther.month)
|| (year == dOther.year && month == dOther.month && day < dOther.day);
}
string Date::toString() //Error: Declaration is incompatible with...declared at line 21...Date.h
{
string s = month + "-" + day; s+="-" + year;
return s;
}
ofstream& operator<<(ofstream& out, Date& d)
{
string s = d.toString();
out.write(s.c_str(), s.size());
return out;
}
void Date::operator=(string s) //no instance of overloaded function "Date::operator=" matches the specified type
{
stringstream stream;
stream << s;
stream >> month;
stream >> day;
stream >> year;
}
Date.h
#ifndef DATE_CLASS
#define DATE_CLASS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
class Date
{
private:
int day, month, year;
public:
Date();
Date(int, int, int);
const int getDay();
const int getMonth();
const int getYear();
string toString();
bool operator<(const Date);
friend ofstream& operator<<(ofstream& out, Date&d);
void operator=(string);
};
#endif
I have no idea why these errors are showing up. Is it a problem with operator overloading? or something with visual studio(for some reason if I delete some code in Date.cpp, the compiler errors disappear)?
You have namespaces mismatch. In the header you do not use the std namespace prefix before string thus the compiler can't find the type string that you need.
I have a problem in returning a struct from a method declared in a class. The struct i is composed by 3 integers: day, month and year.
I set these three values using a method in the class.
struct dmy{
int day, month, year;
};
dmy c;
Then I have a method to return the full struct
dmy Date::getS(){
return c;
}
But I get lots of errors in compiling. What should I do?
I've also read Is it safe to return a struct in C or C++? but I haven't solved my issue.
The program is composed from main.cpp, Difference.h and Date.h
Errors in Date.h :
[Error] 'dmy' does not name a type
Main
using namespace std;
#include <iostream>
#include "Date.h"
int main(int argc, char** argv) {
Date date1;
Date date2;
cout<<"Insert first date\n";
date1.setAll();
return 0;
}
Date.h
class Date{
public:
Date(){
}
~Date(){
}
void setAll();
struct dmy{
int day, month, year;
};
dmy c;
dmy getS();
private:
void setDay();
void setMonth();
void setYear();
};
void Date::setAll(){
setDay();
setMonth();
setYear();
}
void Date::setDay(){
do{
cout<<"Type day: ";
cin>>c.day;
}while(0<c.day<31);
}
void Date::setMonth(){
//didn't right the checking again
cout<<"Type month: ";
cin>>c.month;
}
void Date::setYear(){
cout<<"Type year: ";
cin>>c.year;
}
dmy Date::getS(){
return c;
}
NOTE: The struct was called 'i'
Example
#include <cstdio>
#include <cstdlib>
struct DateInfo {
int day;
int month;
int year;
};
class Date {
public:
Date(const int day, const int month, const int year) : m_info( { day, month, year } )
{
//n/a
}
DateInfo get_info( void ) const
{
return m_info;
}
private:
DateInfo m_info;
};
int main( int, char** )
{
Date date( 29, 02, 1984 );
DateInfo info = date.get_info( );
printf( "%i\n", info.year );
return EXIT_SUCCESS;
}
Build
g++ -std=c++11 -o date date.cpp
instead of
dmy Date::getS(){
return c;
}
write
Date::dmy Date::getS(){
return c;
}
since "dmy" is not in the global namespace.
Try this:
class C1
{
public:
struct S1
{
int a,b,c;
};
S1 f1() { S1 s; return s; }
};
void main()
{
C1 c;
C1::S1 s;
s=c.f1();
}
C1 - class
C1::S1 - struct inside class C1
C1::f1 - function returning C1::S1
I doubt that your source code is compiled in the C style other that C++ style. You could try to change the file extension to ".cpp" instead of ".c" and then gcc will do it's job in C++ style.
Try this
Date.h
#ifndef DATE_H
#define DATE_H
#include<string>
using namespace std;
class Date{
public:
Date(int month, int day, int year);
int getMonth() const;
int getDay() const;
int getYear() const;
private:
int month;
int day;
int year;
};
#endif
Date.cpp
#include "Date.h"
#include<string>
using namespace std;
Date::Date(int month, int day, int year) {
this->month = month;
this->day = day;
this->year = year;
}
int Date::getMonth() const{
return month;
}
int Date::getDay() const{
return day;
}
int Date::getYear() const{
return year;
}
Appointment.cpp
#include "Appointment.h"
#include "Date.h"
#include<sstream>
#include<string>
using namespace std;
Appointnment::Appointment(String description, int month, int day, int yr, int hr, int min)
{
this->description = description;
this->month = month;
this->day = day;
this->yr = yr;
this->hr = hr;
this->min = min;
}
void Appointinment::getDate()
{
//cannot change calling object nor its date object member, just return it
}
}
I'm trying to implement the getDate() function, but I'm having trouble understanding how to return a Date given that this class is specified as an appointment. Are there any resources I can look at to tackle this specific problem?
You code looks good. But when return soemthing, you need either a pointer to Data, or a refernce to Date, or straight object.
Either of following will work:
Date Appointinment::getDate() const
const Date& Appointinment::getDate() const
const Date* Appointinment::getDate() const
Judge from context, maybe first one is what you want.
const Date& Appointinment::getDate()
{
return Date(this->month, this->day, this->yr);
}
this is the most common way.