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.
Related
I have a class Result which takes two other classes UNIT and Date as object parameters, so that I can store the values in them for use in those classes. I will put down what Ive got so far, and a few of the errors I get are as follows.
error: prototype for 'int Result::GetUnit() const' does not match any in class 'Result'|
lab2\Result.h|17|error: candidate is: Result Result::GetUnit() const|
lab2\Result.cpp|66|error: prototype for 'int Result::GetDate()' does not match any in class 'Result'|
lab2\Result.h|18|error: candidate is: Result Result::GetDate() const|
lab2\Result.cpp|73|error: 'SetResult' was not declared in this scope|
error: 'SetResult' was not declared in this scope|
For the error not declared in this scope, after researching I understand that I need to define the functions before the first call is made. I have done that in the .cpp file but I still get the error. What am I doing wrong?
Result.h:
#ifndef RESULT_H
#define RESULT_H
#include <iostream>
#include <string>
#include "UNIT.h"
#include "Date.h"
//const unsigned ResultSize = 10;
using namespace std;
class Result
{
public:
Result(){};
Result(UNIT unitobj1, unsigned marks1, Date dateobj1);
Result GetUnit() const;
Result GetDate() const;
void SetDate(Date dateobj1);
void SetUnit(UNIT unitonj1);
void SetMarks( unsigned marks1 );
unsigned GetMarks() const;
void SetCredits( unsigned cred );
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetResult(istream & input);
void GetResult(ostream & os);//unsigned GetUnit() const;
private:
UNIT unitobj;
Date dateobj;
string id;
int credits;
unsigned marks;
};
inline unsigned Result::GetCredits() const
{
return credits;
}
ostream & operator <<( ostream & os, const Result & S);
istream & operator >>( istream & input, Result & S);
#endif // RESULT_H
Result.cpp:
#include "Result.h"
#include "UNIT.h"
#include "Date.h"
Result::Result(UNIT unitobj1, unsigned marks1, Date dateobj1)
{
unitobj = unitobj1;
marks = marks1;
dateobj = dateobj1;
}
void Result::SetResult(istream &input){
UNIT unitobj1;
unsigned marks1;
Date date1;
input >> unitobj1 >> marks1 >> date1;
SetUnit(unitobj1);
SetMarks(marks1);
SetDate(date1);
}
void Result::GetResult(ostream &os){
os << GetUnit() < " Marks: " << GetMarks() << '\n' << GetDate() << '\n';
}
void Result::SetUnit(UNIT unitobj1){
unitobj = unitobj1;
}
void Result::SetMarks(unsigned marks1){
marks = marks1;
}
void Result::SetDate(Date dateobj1){
dateobj = dateobj1;
}
Result::GetUnit() const{
return unitobj;
}
inline unsigned Result::GetMarks() const{
return marks;
}
Result::GetDate(){
return dateobj;
}
istream & operator >>( istream & input, Result & S)
{
SetResult(input);
return input;
}
ostream & operator <<( ostream & os, const Result & S)
{
GetResult(os);
return os;
}
Date.h:
#if !defined(_DATE_H)
#define _DATE_H
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date();
Date(unsigned day1, string month1, unsigned year1);
void SetDay(unsigned day1);
void SetMonth(string month1);
void SetYear(unsigned year1);
unsigned GetDay() const;
string GetMonth() const;
unsigned GetYear() const;
void SetDate(istream &input);
void GetDate(ostream & os);
private:
unsigned day;
string month;
unsigned year;
};
ostream & operator <<(ostream & os, const Date & D);
istream & operator >>(istream & input, Date & D);
#endif //_DATE_H
Date.cpp:
//
//
// Generated by StarUML(tm) C++ Add-In
#include "Date.h"
Date::Date(unsigned day1, string month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
void Date::SetDay(unsigned day1) {
day = day1;
}
void Date::SetMonth(string month1) {
month = month1;
}
void Date::SetYear(unsigned year1) {
year = year1;
}
inline unsigned Date::GetDay() const {
return day;
}
string Date::GetMonth() const {
return month;
}
inline unsigned Date::GetYear() const {
return year;
}
void Date::SetDate(istream &input){
unsigned day1;
string month1;
unsigned year1;
input >> day1 >> month1 >> year1;
SetDay(day1);
SetMonth(month1);
SetYear(year1);
}
void Date::GetDate(ostream &os){
os << " Date: " << GetDay() << " " << GetMonth() << " " << GetYear();
}
istream & operator >>( istream & input, Date & D) {
SetDate(input);
return input;
}
ostream & operator <<( ostream & os, const Date & D) {
GetDate(os);
return os;
}
UNIT.h:
#ifndef UNIT_H
#define UNIT_H
#include <iostream>
#include <string> // C string library
using namespace std;
const unsigned UnitNameSize = 10;
class UNIT
{
public:
UNIT();
UNIT( string nam, string idd, unsigned cred);
void SetName(string nam);
string GetName() const;
void SetMarks(unsigned marks1);
unsigned GetMarks();
void SetCredits(unsigned cred);
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetUnit(istream & input);
void GetUnit(ostream & os);
private:
string name;
string id;
unsigned marks;
int credits;
};
ostream & operator <<( ostream & os, const UNIT & U);
istream & operator >>( istream & input, UNIT & U);
#endif // UNIT_H
UNIT.cpp:
#include "UNIT.h"
#include <string>
UNIT::UNIT()
{
name[0] = '\0';
}
void UNIT::SetName(string nam)
{
name = nam;
}
string UNIT::GetName() const
{
return name;
}
void UNIT::SetID(string idd)
{
id = idd;
}
string UNIT::GetID() const
{
return id;
}
void UNIT::SetCredits(unsigned cred){
credits = cred;
}
inline unsigned UNIT::GetCredits() const{
return credits;
}
UNIT::UNIT( string nam, string idd,
unsigned cred)
{
name.replace(0, 10, nam );
id = idd;
credits = cred;
}
void UNIT::SetUnit(istream &input){
string nam;
string idd;
unsigned cred;
getline(input,nam, '\n');
getline(input,idd,'\n');
input >> cred;
SetName(nam);
SetID(idd);
SetCredits(cred);
}
void UNIT::GetUnit(ostream & os){
os << " Unit ID: " << GetID() << '\n'
<< " Unit Name: " << GetName() << '\n'
<< " Credits: " << GetCredits() << '\n';
}
istream & operator >>( istream & input, UNIT & U)
{
SetUnit(input);
return input;
}
ostream & operator <<( ostream & os, const UNIT & U)
{
GetUnit(os);
return os;
}
Note: I know that I can just make the overloaded operators methods of the class and declare them as friends, but I am trying to achieve this by using set and get methods. Any help would be very appreciated!
So, this is pretty straightforward, look at Result::GetUnit
Result::GetUnit() const {
return unitobj;
}
This method is missing a return type. Now look at unitobj
UNIT unitobj;
So it's clear that the return type should be UNIT, so the above method should be defined as
UINT Result::GetUnit() const {
return unitobj;
}
Now look at how you declared this method in your class
class Result
{
...
Result GetUnit() const;
Here you gave it a return type of Result where we've already seen that the return type should be UINT, so change the above to
class Result
{
...
UNIT GetUnit() const;
Result::GetDate has similar problems, here the return type should be Date but again you specified it as Result.
For the SetResult error you simply need to specify that you are calling the SetResult method on a Result object. The compiler thinks you are calling a global SetResult function (which doesn't exist)
Like this
istream & operator >>( istream & input, Result & S)
{
S.SetResult(input);
return input;
}
S.SetResult(input); tells the compiler that you want to call the SetResult method using the Result object refered to by S.
You can tell this by reading the error message carefully, it said 'SetResult' was not declared in this scope, not 'Result::SetResult' was not declared in this scope. As you said you have declared Result::SetResult, but your code wasn't calling that, it was trying to call a different function called SetResult and the compiler correctly reported that no such function was declared.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi I have a c++ class called Date. The codes are below. How to create a test run for the class? Am very much new to C++. I have searched a lot but couldn't able find any fruitful/useful websites for that. Please someone can help? Thanks in advance.
date.h
#ifndef H_date
#define H_date
#include <iostream>
using namespace std;
class Date{
friend ostream &operator << (ostream & os, const Date &);
friend istream &operator >> (istream & is, Date &);
public:
Date();
Date(int day, int month, int year);
void setday(int day);
void setmonth(int month);
void setyear(int year);
void setDate(int day, int month, int year);
int getday() const;
int getmonth() const;
int getyear() const;
void print() const;
protected:
int day;
int month;
int year;
};
#endif
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
Date::Date()
{
day = 0;
month = 0;
year = 0;
}
Date::Date(int newDay, int newMonth, int newYear)
{
day = newDay;
month = newMonth;
year = newYear;
}
void Date::setday(int newDay)
{
day =newDay;
}
void Date::setmonth(int newMonth)
{
month = newMonth;
}
void Date::setyear(int newYear)
{
year = newYear;
}
void Date::setDate(int newDay, int newMonth, int newYear)
{
day =newDay;
month = newMonth;
year = newYear;
}
int Date::getday()const
{
return day;
}
int Date::getmonth()const
{
return month;
}
int Date::getyear()const
{
return year;
}
void Date::print()const
{
cout << day << ":" << month<< ":" << year <<endl;
}
ostream& operator<< (ostream& osObject, const Date& date1)
{
osObject << date1.day
<< ":" <<date1.month
<< ":" << date1.year;
return osObject;
}
istream& operator>> (istream& isObject, Date& date1)
{
isObject>>date1.day>>date1.month>>date1.year;
return isObject;
}
For unit tests, you would typically use a unit-test framework such as CppUnit:
For each class, you create a series of test;
For each method to be tested, you develop a dedicated test;
Each test verifies some assertions using functions or macros such as CPPUNIT_ASSERT, CPPUNIT_FAIL, CPPUNIT_ASSERT_THROW;
It's often useful to make the tester class friend to the tested class.
Here is an example for testing your constructors:
void DateUnitTest::ConstructorTests
{
// Test default-ctor
{
Date date;
CPPUNIT_ASSERT ( date.day == 0 );
CPPUNIT_ASSERT ( date.month == 0 );
CPPUNIT_ASSERT ( date.year == 0 );
}
// Test ctor with args
{
Date date(31,12,2015);
CPPUNIT_ASSERT ( date.day == 31 );
CPPUNIT_ASSERT ( date.month == 12 );
CPPUNIT_ASSERT ( date.year == 2015 );
}
}
i am new to c++ and not skillful in English!
i have searched web and read same posts on this site but did not worked for me!
its a wonder that inheritance used in this part of my project seems correct to me but get these error from C::B 13.12 with GNU GCC Compiler under Windows 8.1 :
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
D:\AirportProject\Airport\aircrew.h|8|error: expected class-name before '{' token|
D:\AirportProject\Airport\aircrew.cpp||In constructor 'Aircrew::Aircrew(const string&, const string&, long long int, const Date&, long long int, const Date&)':|
D:\AirportProject\Airport\aircrew.cpp|5|error: class 'Aircrew' does not have any field named 'Person'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
this is my code:
person.h:
#include "date.h"
using namespace std;
class Person{
public:
Person( const string&, const string&, long long, const Date& );
void set( const string&, const string&, long long, const Date& );
private:
char name [31];
char last_name [31];
long long national_code ; //long long for storing a 10 digit number
Date birthday;
};
person.cpp:
#include "person.h"
#include <string>
using namespace std;
Person::Person( const string& n, const string&ln, long long ncode, const Date& D )
{ set( n, ln, ncode, D ); }
//set data members of aircrew
void Person::set( const string& n, const string&ln, long long ncode, const Date& D )
{
birthday = D ;
strncpy( name, n.c_str(), 30 ); //converting string to char array
strncpy( last_name, ln.c_str(), 30 );
national_code= ncode;
}
aircrew.h:
#include <string>
#include "person.h"
#include "date.h"
using namespace std;
class Aircrew: public Person
{
public:
//copy-constructor
Aircrew( const string&, const string&, long long, const Date&,
long long, const Date& );
//Set member data
void set( long long, const Date& );
private:
//long long used to be sure about 10digit numbers and more
long long personal_code;
// date of employment
Date employ_date;
};
aircrew.cpp:
#include "aircrew.h"
using namespace std;
Aircrew::Aircrew( const string& n, const string& ln, long long ncode,
const Date& D_b, long long pcode,const Date& D_e )
:Person( n, ln, ncode, D_b ) //base class constructor
{set( pcode, D_e );}
void Aircrew::set(long long pcode, const Date& D_e )
{
personal_code = pcode;
employ_date = D_e; //assignment operator( = ) is overloaded for Date class
}
date.h
class Date
{
public:
Date ( int=1, int=1, int=1900 ); //Default constructor
void set( int, int, int );
int getForSearch() const ;
void print() const ;
//assignment operator( = ) is overloaded for Date class
Date& operator=( const Date& D )//assignment operator(=) can NOT be defined as friend
{
day = D.day;
month = D.month;
year = D.year;
return *this;
}
private:
int day, month, year;
};
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
//Default constructor
Date::Date( int d, int m, int y) { set( d, m, y ); }
//set() function sets the entered date if its valid
void Date::set( int d, int m, int y )
{
day=( d>0? d: 1);
month=( m>0? m: 1);
year= ( y>0? y: 1900);
}
//getForSearch () returns an integer like yyyymmdd for easy search
int Date::getForSearch () const
{
return ((year*100 + month)*100 + day);
}
//print Date in the form of mm/dd/yyyy
void Date::print () const
{
cout << " " << month << "/" << day << "/" << year;
}
and also an empty main.cpp!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << right <<setw( 30 ) << "\nAIRPORT MANAGER" << endl;
return 0;
}
Any help will be appreciate :)
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.