转换为非标量类型错误 [英] Conversion to non-scalar type error

查看:126
本文介绍了转换为非标量类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的程序(我是菜鸟),该程序从txt文件读取生日,通过用户输入输入一个新的生日,然后打印出一周内即将到来的生日.然后将信息打印回txt文件.我一直收到以下错误:从'Datee *'转换为非标量类型'Datee'的请求.我知道这与指针有关,但是我被卡住了.请事先提供帮助和感谢.

更新:第一个错误是在Albert的帮助下修复的.现在出现了一个新的编译错误:在"{"令牌之前请求非聚合类型的成员.尚未声明"findDays"".发生的两条线如下所示.

更新:第二个错误已修复.该程序现在基本上可以正常工作了……只需要进行一些调整和清理即可.感谢您的帮助!
错误1:请求从日期*"转换为非标量类型日期"."
错误2:在"{"令牌之前请求非聚合类型的成员.未声明"findDays".


//main.cpp

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



//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




//date.cpp

#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

推荐答案

operator new将返回一个指向对象的指针,因此您需要为其提供一个指针以将其存储到:
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;


这篇关于转换为非标量类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆