Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to make a simple program (I'm a noob) that reads in birthdays from a txt file, takes one new birthday in via user input, and then prints out birthdays that are upcoming within the week. It then prints the info back to the txt file. I keep getting the following error: "conversion from 'Datee*' to non-scalar type 'Datee' requested. I know that has something to do with pointers but I'm stuck. Please help and thanks in advance.

UPDATE: The first error was fixed with the help of Albert. There is now a new compile error: "request for member of non-aggregate type before '{' token. 'findDays' has not been declared". The two lines where it occurs are indicated below.

UPDATE: Second error fixed. The program is basically working now...just some tweaking and cleanup left. Thanks for the help!
Error 1: "conversion from 'Datee*' to non-scalar type 'Datee' requested."
Error 2: "request for member of non-aggregate type before '{' token. 'findDays' has not been declared".


//main.cpp

#include <iostream>
#include <fstream>
#include <string.h>
#include <cstring>
#include <ctime>
#include <sstream>
#include <vector>
#include "datee.h"


int main(int argc, char *argv[])
{ 
  using namespace std;
 
  char dates [10];
  _strdate(dates);
  char *tkn;
  string name[100], month[100], day[100]; 
  int imonth, iday, cMonth, cDay, birthDays, currentDays;
  int Months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  string smonth, sday, sname;
 
  ///tokenize
  tkn = strtok(dates, "/");  
  imonth = atoi(tkn);
       
  tkn = strtok(NULL, "/");     
  iday = atoi(tkn);
  
  ///create date object for current time        
  Datee *currentDate = new Datee(iday, imonth);  //UPDATE: Changed 'currentDate' to '*currentDate' which fixed error 1
  
  ///read file into array
  ifstream reader;
  reader.open("birthday.txt");
  string tmp;
  int x = 0;
  
  while(reader.good())
  {
      getline(reader, tmp);
      name[x] = tmp;
      getline(reader, tmp);
      month[x] = tmp;
      getline(reader, tmp);
      day[x] = tmp;
      x++;   
  }
 
  
  ///enter new name/date and validate 
  cout << "enter a name: ";
  getline(cin, sname);
  
  cout << "enter a month(no zeros in front), or press <enter> to skip: ";
  getline(cin, smonth);
  stringstream(smonth) >> imonth;
  
  while(imonth < 1 || imonth > 12)
  {
      cout << "wrong!  reenter: ";
      getline(cin, smonth);
      stringstream(smonth) >> imonth;
  }
  
  cout << "enter a day(no zeros in front), or press <enter> to skip: ";
  getline(cin, sday);
  stringstream(sday) >> iday;
  
  while(iday < 1 || iday > Months[imonth-1])
  {
      cout << "wrong!  reenter: ";
      getline(cin, sday);
      stringstream(sday) >> iday;
  }
 
 
 ///write new date to array 
 int t = x-1;
 name[t] = sname;
 month[t] = smonth;
 day[t] = sday; 
 
 
 ///write array to text file
 ofstream writer("birthday.txt");
 
 for(int g = x; g >= 0; g--)
 {
     writer << name[g] << endl;
     writer << month[g] << endl;
     writer << day[g] << endl;
 }


  ///check array for days until b-day!!! 
 int daysUntil = 0;
 int h = x;
 do
 {     
      smonth = month[h];    
      stringstream(smonth) >> imonth;
      sday = day[h];
      stringstream(sday) >> iday;
      
      Datee *birthDate = new Datee(iday, imonth);  //UPDATE: changed 'birthDate' to '*birthDate' which fixed error 1
      
      birthDays = birthDate.findDays();  //UPDATE: changed to 'birthDate->findDays();' which fixed error 2

      currentDays = currentDate.findDays();  //UPDATE: changed to 'currentDate->findDays();' which fixed error 2
      
      daysUntil =  birthDays - currentDays;
      
      if(daysUntil >= 0 && daysUntil <= 7)
      {
          cout << "Birthday is coming soon!  " << daysUntil << "  days to go until " << name[x] << "'s birthday!" << endl;
      }
      h--;
      
 }while(h >= 0);
 
  system("PAUSE");
  return 0;
}
}</enter></enter></vector></sstream></ctime></cstring></string.h></fstream></iostream>


//datee.h

<pre lang="cs">#ifndef DATEE_H_
#define DATEE_H_
<pre lang="cs">class Datee{
 public:
     Datee(int idays, int imths);
     int findDays();
 private:
     int months[12];
     int imths, idays, currentMonth, currentDay, totalDays;
};
#endif



//date.cpp

C#
#include "datee.h"
Datee::Datee(int day, int mth)
{
    imths = mth;
    idays = day;
}
int Datee::findDays()
 {
   months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   totalDays = 0;
   for(int i = 0; i < imths; i++)
    {
        totalDays += months[i];
    }
 totalDays += idays;
 return totalDays;
 }  //total days in year so far as of today
Posted
Updated 19-May-11 14:14pm
v4
Comments
Member 12770978 14-Oct-16 9:59am    
How to solve if i got these error in c++.. conversion from

1 solution

operator new will return a pointer to an object, so you need to give it a pointer to store it to:
CClass *myClass = new CClass();

since you're allocating this on the heap, you will need to deallocate by hand so you don't have memory leaks:
delete myClass;
 
Share this answer
 
Comments
thomas struss 19-May-11 18:22pm    
Awesome, that got rid of the error. Although now I get a new one: "request for member of non-aggregate type before '{' token. 'findDays' has not been declared" If you could help with this new one that would be really nice, if not I will keep searching the net.
Albert Holguin 19-May-11 21:34pm    
Post it as a new question and post the error along with line where the error is indicated and I'll check it out...
Albert Holguin 19-May-11 21:37pm    
Oh, looks like you fixed your other error already?
thomas struss 19-May-11 22:57pm    
Ya, I did. I'm not familiar with these forums so I'm sorry if it didn't send anything to your email even though you are the answerer of this question. Thanks a lot for the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900