Gunnlaugur
June 14th, 2010, 03:12 AM
Hello.
I'm working on a project in the university and I have done most of the code but when I try to compile it I keep getting the following errors. I've googled it, but I cant seem to find the exact problem and fix for it.
Any help would be greatly appreciated.
**** Build of configuration Debug for project HLUT4 ****
make all
Building file: ../src/Date.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Date.d" -MT"src/Date.d" -o"src/Date.o" "../src/Date.cpp"
In file included from ../src/Date.cpp:1:
../src/Date.h:8: error: expected ‘)’ before ‘t’
../src/Date.cpp:44: error: prototype for ‘Date::Date(time_t)’ does not match any in class ‘Date’
../src/Date.h:9: error: candidates are: Date::Date(const Date&)
../src/Date.cpp:14: error: Date::Date(int, int, int)
../src/Date.cpp:5: error: Date::Date()
../src/Date.cpp: In static member function ‘static Date Date::Current()’:
../src/Date.cpp:103: error: no matching function for call to ‘Date::Date(time_t)’
../src/Date.cpp:58: note: candidates are: Date::Date(const Date&)
../src/Date.cpp:14: note: Date::Date(int, int, int)
../src/Date.cpp:5: note: Date::Date()
make: *** [src/Date.o] Error 1
My code:
HLUT4.cpp
#include <assert.h>
#include <string>
#include "Date.h"
#include "SmartPtrRL.h"
#include <iostream>
#include <stdlib.h>
//#include "MemLeakTest.h"
using namespace std;
//#ifdef _DEBUG
//#define new DEBUG_NEW
//#endif
template <typename T>
SmartPtrRL<T> UseSmartPtr( SmartPtrRL<T> ptr )
{
std::cout << *ptr;
return ptr;
}
template <typename T>
void CreateSmartPointers( )
{
// Tómur SmartPtrRL á að virka:
SmartPtrRL<T> emptyPtr;
// Búum til eina "klíku" með tveim meðlimum:
SmartPtrRL<T> ptr1( new T( ) );
SmartPtrRL<T> ptr2 = ptr1;
// "Assignment to self" ætti að vera noop:
ptr2 = ptr2;
// SmartPtrRL ætti að virka bæði sem færibreyta og sem skilagildi:
SmartPtrRL<T> ptr3 = UseSmartPtr( ptr1 );
// Búum til aðra SmartPtrRL "klíku":
SmartPtrRL<T> ptrA( new T( ) );
SmartPtrRL<T> ptrB( ptrA );
SmartPtrRL<T> ptrC( ptrA );
SmartPtrRL<T> ptrD( ptrB );
SmartPtrRL<T> ptrE( ptrD );
// látum svo einn skipta um klíku:
ptrC = ptr2;
}
int main( int argc, char* argv[] )
{
cout << "bleh" << endl;
CreateSmartPointers< Date >( );
CreateSmartPointers< std::string >( );
// Prófum að lokum hvort operator -> virki rétt:
SmartPtrRL<std::string> ptrString( new std::string( "Nú er gaman" ) );
assert( ptrString->length( ) == 11 );
return 0;
}
Date.h
#include <iosfwd>
class Date {
public:
Date();
Date(int y, int m, int d);
Date(time_t t);
Date(const Date& rhs);
Date& operator =(const Date& rhs);
~Date();
bool operator ==(const Date& rhs) const;
bool operator <(const Date& rhs) const;
bool operator >(const Date& rhs) const;
bool operator >=(const Date& rhs) const;
bool operator <=(const Date& rhs) const;
static Date Current();
int Compare(const Date& rhs) const;
bool IsValid() const;
bool IsLeapYear() const;
int GetYear() const;
int GetMonth() const;
int GetDay() const;
private:
int m_nYear;
int m_nMonth;
int m_nDay;
};
std::ostream& operator <<(std::ostream& lhs, const Date& rhs);
Note: I get these errors in Date.h
"expected ‘)’ before ‘t’" on this decloration Date(time_t t);
and this forDate(const Date&) I get "candidates are: Date::Date(const Date&)"
Date.cpp
#include "Date.h"
#include "stdio.h"
#include <ostream>
Date::Date() {
m_nYear = 0;
m_nMonth = 0;
m_nDay = 0;
}
Date::~Date() {
}
Date::Date(int y, int m, int d) :
m_nYear(y), m_nMonth(m), m_nDay(d) {
if (m_nMonth < 1 || m_nMonth > 12) {
m_nMonth = 0;
}
if (m_nDay < 1 || m_nDay > 31) {
m_nDay = 0;
}
if (m_nMonth == 2) // Hér ætti að sjálfsögðu að nota enum eða fasta (const)!
{
if (IsLeapYear()) {
if (m_nDay > 29) {
m_nDay = 0;
}
} else {
if (m_nDay > 28) {
m_nDay = 0;
}
}
}
if (m_nMonth == 4 || m_nMonth == 6 || m_nMonth == 9 || m_nMonth == 11) {
if (m_nDay > 30) {
m_nDay = 0;
}
}
}
Date::Date(time_t t) {
tm* pTm = localtime(&t);
if (pTm != NULL) {
m_nYear = 1900 + pTm->tm_year;
m_nMonth = 1 + pTm->tm_mon;
m_nDay = pTm->tm_mday;
} else {
m_nYear = 0;
m_nMonth = 0;
m_nDay = 0;
}
}
Date::Date(const Date& rhs) {
printf("Date(const Date& rhs)\n");
m_nYear = rhs.m_nYear;
m_nMonth = rhs.m_nMonth;
m_nDay = rhs.m_nDay;
}
Date& Date::operator =(const Date& rhs) {
printf("operator = (const Date& rhs)\n");
if (&rhs != this) {
m_nYear = rhs.m_nYear;
m_nMonth = rhs.m_nMonth;
m_nDay = rhs.m_nDay;
}
return *this;
}
bool Date::operator ==(const Date& rhs) const {
printf("operator == (const Date& rhs) const\n");
return Compare(rhs) == 0;
}
bool Date::operator <(const Date& rhs) const {
printf("operator < (const Date& rhs) const\n");
return Compare(rhs) < 0;
}
bool Date::operator >(const Date& rhs) const {
printf("operator > (const Date& rhs) const\n");
return Compare(rhs) > 0;
}
bool Date::operator >=(const Date& rhs) const {
printf("operator >= (const Date& rhs) const\n");
return Compare(rhs) >= 0;
}
bool Date::operator <=(const Date& rhs) const {
printf("operator <= (const Date& rhs) const\n");
return Compare(rhs) <= 0;
}
Date Date::Current() {
printf("Current()\n");
return Date(time(NULL));
}
int Date::Compare(const Date& rhs) const {
printf("Compare(const Date& rhs) const\n");
if (m_nYear < rhs.m_nYear) {
return -1;
} else if (m_nYear > rhs.m_nYear) {
return 1;
} else if (m_nMonth < rhs.m_nMonth) {
return -1;
} else if (m_nMonth > rhs.m_nMonth) {
return 1;
} else if (m_nDay < rhs.m_nDay) {
return -1;
} else if (m_nDay > rhs.m_nDay) {
return 1;
}
return 0;
}
bool Date::IsValid() const {
if (m_nYear == 0 || m_nMonth == 0 || m_nDay == 0) {
return false;
}
return true;
}
bool Date::IsLeapYear() const {
if (m_nYear % 400 == 0) {
return true;
}
if (m_nYear % 100 == 0) {
return false;
}
if (m_nYear % 4 == 0) {
return true;
}
return false;
}
int Date::GetYear() const {
printf("GetYear() const\n");
return m_nYear;
}
int Date::GetMonth() const {
printf("GetMonth() const\n");
return m_nMonth;
}
int Date::GetDay() const {
printf("GetDay() const\n");
return m_nDay;
}
std::ostream& operator <<(std::ostream& lhs, const Date& rhs) {
lhs.fill('0');
lhs.width('2');
lhs << rhs.GetYear() << "-" << rhs.GetMonth() << "-" << rhs.GetDay();
return lhs;
}
Note: I get these errors in Date.cpp
Date::Date()
Date::Date(int, int, int)
prototype for ‘Date::Date(time_t)’ does not match any in class ‘Date’
no matching function for call to ‘Date::Date(time_t)’ on this "return Date(time(NULL));" in Date Date::Current()
And then my SmartPtrRL.h
/*
* SmartPtrRL.h
*
* Created on: Jun 11, 2010
* Author: gunnl
*/
#ifndef SMARTPTRRL_H_
#define SMARTPTRRL_H_
#include <string>
#include <iostream>
using namespace std;
template<typename T>
class SmartPtrRL {
public:
/* constructors */
SmartPtrRL();
SmartPtrRL(SmartPtrRL<T>& rhs);
SmartPtrRL(T* ptr);
/* destructor */
~SmartPtrRL();
/* operators */
T* operator->();
T& operator*();
SmartPtrRL<T>& operator =(SmartPtrRL<T>& rhs);
private:
T* _pointee;
SmartPtrRL* _next;
SmartPtrRL* _prev;
};
template<class T>
SmartPtrRL<T>::SmartPtrRL() {
_pointee = NULL;
_next = NULL;
_prev = NULL;
}
template<typename T>
SmartPtrRL<T>::SmartPtrRL(SmartPtrRL<T>& rhs) {
_pointee = rhs._pointee;
_prev = &rhs;
_next = NULL;
rhs._next = this;
}
template<typename T>
SmartPtrRL<T>::SmartPtrRL(T* ptr) {
_pointee = ptr;
_next = NULL;
_prev = NULL;
}
template<typename T>
SmartPtrRL<T>::~SmartPtrRL() {
// smart pointerinn bendir ekki lengur á neitt, því óhætt að eyða pointernum
if ((_next == NULL) && (_prev == NULL)) {
delete _pointee;
} else { // annars verður að láta fyrri benda á næsta pointer þar á eftir
_prev->_next = _next;
// og svo ef pointerinn næsta á þá verður að
if (_next != NULL) {
_next->_prev = _prev;
}
}
}
template<typename T>
T& SmartPtrRL<T>::operator*() {
return *_pointee;
}
template<typename T>
T* SmartPtrRL<T>::operator->() {
return _pointee;
}
// glæra 15 í pakka 10 - reference linking
template<typename T>
SmartPtrRL<T>& SmartPtrRL<T>::operator =(SmartPtrRL<T>& rhs) {
// ef þetta tilvik og það sem er hægra megin eru ekki sama tilvikið þá
// þurfum við að láta this taka gildi af rhs annars skilum við þessu tilviki
if (this != &rhs) {
// smart pointerinn bendir ekki lengur á neitt, því óhætt að eyða pointernum
if (_next == NULL && _prev == NULL) {
delete _pointee;
} else { // annars verður að láta fyrri benda á næsta pointer þar á eftir
rhs._prev->_next = _next;
if (_next != NULL) { // og svo tengja pointernum inn í keðjuna
rhs._next->_prev = _prev;
}
}
_pointee = rhs._pointee;
_prev = &rhs;
_next = NULL;
rhs._next = this;
}
return *this;
}
#endif /* SMARTPTRRL_H_ */
Thanks
-Gunnlaugur
I'm working on a project in the university and I have done most of the code but when I try to compile it I keep getting the following errors. I've googled it, but I cant seem to find the exact problem and fix for it.
Any help would be greatly appreciated.
**** Build of configuration Debug for project HLUT4 ****
make all
Building file: ../src/Date.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Date.d" -MT"src/Date.d" -o"src/Date.o" "../src/Date.cpp"
In file included from ../src/Date.cpp:1:
../src/Date.h:8: error: expected ‘)’ before ‘t’
../src/Date.cpp:44: error: prototype for ‘Date::Date(time_t)’ does not match any in class ‘Date’
../src/Date.h:9: error: candidates are: Date::Date(const Date&)
../src/Date.cpp:14: error: Date::Date(int, int, int)
../src/Date.cpp:5: error: Date::Date()
../src/Date.cpp: In static member function ‘static Date Date::Current()’:
../src/Date.cpp:103: error: no matching function for call to ‘Date::Date(time_t)’
../src/Date.cpp:58: note: candidates are: Date::Date(const Date&)
../src/Date.cpp:14: note: Date::Date(int, int, int)
../src/Date.cpp:5: note: Date::Date()
make: *** [src/Date.o] Error 1
My code:
HLUT4.cpp
#include <assert.h>
#include <string>
#include "Date.h"
#include "SmartPtrRL.h"
#include <iostream>
#include <stdlib.h>
//#include "MemLeakTest.h"
using namespace std;
//#ifdef _DEBUG
//#define new DEBUG_NEW
//#endif
template <typename T>
SmartPtrRL<T> UseSmartPtr( SmartPtrRL<T> ptr )
{
std::cout << *ptr;
return ptr;
}
template <typename T>
void CreateSmartPointers( )
{
// Tómur SmartPtrRL á að virka:
SmartPtrRL<T> emptyPtr;
// Búum til eina "klíku" með tveim meðlimum:
SmartPtrRL<T> ptr1( new T( ) );
SmartPtrRL<T> ptr2 = ptr1;
// "Assignment to self" ætti að vera noop:
ptr2 = ptr2;
// SmartPtrRL ætti að virka bæði sem færibreyta og sem skilagildi:
SmartPtrRL<T> ptr3 = UseSmartPtr( ptr1 );
// Búum til aðra SmartPtrRL "klíku":
SmartPtrRL<T> ptrA( new T( ) );
SmartPtrRL<T> ptrB( ptrA );
SmartPtrRL<T> ptrC( ptrA );
SmartPtrRL<T> ptrD( ptrB );
SmartPtrRL<T> ptrE( ptrD );
// látum svo einn skipta um klíku:
ptrC = ptr2;
}
int main( int argc, char* argv[] )
{
cout << "bleh" << endl;
CreateSmartPointers< Date >( );
CreateSmartPointers< std::string >( );
// Prófum að lokum hvort operator -> virki rétt:
SmartPtrRL<std::string> ptrString( new std::string( "Nú er gaman" ) );
assert( ptrString->length( ) == 11 );
return 0;
}
Date.h
#include <iosfwd>
class Date {
public:
Date();
Date(int y, int m, int d);
Date(time_t t);
Date(const Date& rhs);
Date& operator =(const Date& rhs);
~Date();
bool operator ==(const Date& rhs) const;
bool operator <(const Date& rhs) const;
bool operator >(const Date& rhs) const;
bool operator >=(const Date& rhs) const;
bool operator <=(const Date& rhs) const;
static Date Current();
int Compare(const Date& rhs) const;
bool IsValid() const;
bool IsLeapYear() const;
int GetYear() const;
int GetMonth() const;
int GetDay() const;
private:
int m_nYear;
int m_nMonth;
int m_nDay;
};
std::ostream& operator <<(std::ostream& lhs, const Date& rhs);
Note: I get these errors in Date.h
"expected ‘)’ before ‘t’" on this decloration Date(time_t t);
and this forDate(const Date&) I get "candidates are: Date::Date(const Date&)"
Date.cpp
#include "Date.h"
#include "stdio.h"
#include <ostream>
Date::Date() {
m_nYear = 0;
m_nMonth = 0;
m_nDay = 0;
}
Date::~Date() {
}
Date::Date(int y, int m, int d) :
m_nYear(y), m_nMonth(m), m_nDay(d) {
if (m_nMonth < 1 || m_nMonth > 12) {
m_nMonth = 0;
}
if (m_nDay < 1 || m_nDay > 31) {
m_nDay = 0;
}
if (m_nMonth == 2) // Hér ætti að sjálfsögðu að nota enum eða fasta (const)!
{
if (IsLeapYear()) {
if (m_nDay > 29) {
m_nDay = 0;
}
} else {
if (m_nDay > 28) {
m_nDay = 0;
}
}
}
if (m_nMonth == 4 || m_nMonth == 6 || m_nMonth == 9 || m_nMonth == 11) {
if (m_nDay > 30) {
m_nDay = 0;
}
}
}
Date::Date(time_t t) {
tm* pTm = localtime(&t);
if (pTm != NULL) {
m_nYear = 1900 + pTm->tm_year;
m_nMonth = 1 + pTm->tm_mon;
m_nDay = pTm->tm_mday;
} else {
m_nYear = 0;
m_nMonth = 0;
m_nDay = 0;
}
}
Date::Date(const Date& rhs) {
printf("Date(const Date& rhs)\n");
m_nYear = rhs.m_nYear;
m_nMonth = rhs.m_nMonth;
m_nDay = rhs.m_nDay;
}
Date& Date::operator =(const Date& rhs) {
printf("operator = (const Date& rhs)\n");
if (&rhs != this) {
m_nYear = rhs.m_nYear;
m_nMonth = rhs.m_nMonth;
m_nDay = rhs.m_nDay;
}
return *this;
}
bool Date::operator ==(const Date& rhs) const {
printf("operator == (const Date& rhs) const\n");
return Compare(rhs) == 0;
}
bool Date::operator <(const Date& rhs) const {
printf("operator < (const Date& rhs) const\n");
return Compare(rhs) < 0;
}
bool Date::operator >(const Date& rhs) const {
printf("operator > (const Date& rhs) const\n");
return Compare(rhs) > 0;
}
bool Date::operator >=(const Date& rhs) const {
printf("operator >= (const Date& rhs) const\n");
return Compare(rhs) >= 0;
}
bool Date::operator <=(const Date& rhs) const {
printf("operator <= (const Date& rhs) const\n");
return Compare(rhs) <= 0;
}
Date Date::Current() {
printf("Current()\n");
return Date(time(NULL));
}
int Date::Compare(const Date& rhs) const {
printf("Compare(const Date& rhs) const\n");
if (m_nYear < rhs.m_nYear) {
return -1;
} else if (m_nYear > rhs.m_nYear) {
return 1;
} else if (m_nMonth < rhs.m_nMonth) {
return -1;
} else if (m_nMonth > rhs.m_nMonth) {
return 1;
} else if (m_nDay < rhs.m_nDay) {
return -1;
} else if (m_nDay > rhs.m_nDay) {
return 1;
}
return 0;
}
bool Date::IsValid() const {
if (m_nYear == 0 || m_nMonth == 0 || m_nDay == 0) {
return false;
}
return true;
}
bool Date::IsLeapYear() const {
if (m_nYear % 400 == 0) {
return true;
}
if (m_nYear % 100 == 0) {
return false;
}
if (m_nYear % 4 == 0) {
return true;
}
return false;
}
int Date::GetYear() const {
printf("GetYear() const\n");
return m_nYear;
}
int Date::GetMonth() const {
printf("GetMonth() const\n");
return m_nMonth;
}
int Date::GetDay() const {
printf("GetDay() const\n");
return m_nDay;
}
std::ostream& operator <<(std::ostream& lhs, const Date& rhs) {
lhs.fill('0');
lhs.width('2');
lhs << rhs.GetYear() << "-" << rhs.GetMonth() << "-" << rhs.GetDay();
return lhs;
}
Note: I get these errors in Date.cpp
Date::Date()
Date::Date(int, int, int)
prototype for ‘Date::Date(time_t)’ does not match any in class ‘Date’
no matching function for call to ‘Date::Date(time_t)’ on this "return Date(time(NULL));" in Date Date::Current()
And then my SmartPtrRL.h
/*
* SmartPtrRL.h
*
* Created on: Jun 11, 2010
* Author: gunnl
*/
#ifndef SMARTPTRRL_H_
#define SMARTPTRRL_H_
#include <string>
#include <iostream>
using namespace std;
template<typename T>
class SmartPtrRL {
public:
/* constructors */
SmartPtrRL();
SmartPtrRL(SmartPtrRL<T>& rhs);
SmartPtrRL(T* ptr);
/* destructor */
~SmartPtrRL();
/* operators */
T* operator->();
T& operator*();
SmartPtrRL<T>& operator =(SmartPtrRL<T>& rhs);
private:
T* _pointee;
SmartPtrRL* _next;
SmartPtrRL* _prev;
};
template<class T>
SmartPtrRL<T>::SmartPtrRL() {
_pointee = NULL;
_next = NULL;
_prev = NULL;
}
template<typename T>
SmartPtrRL<T>::SmartPtrRL(SmartPtrRL<T>& rhs) {
_pointee = rhs._pointee;
_prev = &rhs;
_next = NULL;
rhs._next = this;
}
template<typename T>
SmartPtrRL<T>::SmartPtrRL(T* ptr) {
_pointee = ptr;
_next = NULL;
_prev = NULL;
}
template<typename T>
SmartPtrRL<T>::~SmartPtrRL() {
// smart pointerinn bendir ekki lengur á neitt, því óhætt að eyða pointernum
if ((_next == NULL) && (_prev == NULL)) {
delete _pointee;
} else { // annars verður að láta fyrri benda á næsta pointer þar á eftir
_prev->_next = _next;
// og svo ef pointerinn næsta á þá verður að
if (_next != NULL) {
_next->_prev = _prev;
}
}
}
template<typename T>
T& SmartPtrRL<T>::operator*() {
return *_pointee;
}
template<typename T>
T* SmartPtrRL<T>::operator->() {
return _pointee;
}
// glæra 15 í pakka 10 - reference linking
template<typename T>
SmartPtrRL<T>& SmartPtrRL<T>::operator =(SmartPtrRL<T>& rhs) {
// ef þetta tilvik og það sem er hægra megin eru ekki sama tilvikið þá
// þurfum við að láta this taka gildi af rhs annars skilum við þessu tilviki
if (this != &rhs) {
// smart pointerinn bendir ekki lengur á neitt, því óhætt að eyða pointernum
if (_next == NULL && _prev == NULL) {
delete _pointee;
} else { // annars verður að láta fyrri benda á næsta pointer þar á eftir
rhs._prev->_next = _next;
if (_next != NULL) { // og svo tengja pointernum inn í keðjuna
rhs._next->_prev = _prev;
}
}
_pointee = rhs._pointee;
_prev = &rhs;
_next = NULL;
rhs._next = this;
}
return *this;
}
#endif /* SMARTPTRRL_H_ */
Thanks
-Gunnlaugur