c++ - 非类类型成员打印错误请求 'char'

标签 c++ arrays pointers printing char

每次我尝试运行打印功能时,它都会告诉我:

SalesDB.cpp:61:19: error: request for member ‘print’ in ‘((SalesDB*)this)->SalesDB::salesPerson[i]’, which is of non-class type ‘char’
salesPerson[i].print();
               ^

我似乎不知道如何解决这个问题。我包含了所有需要的代码。让我知道是否需要更多。

销售数据库.h

#ifndef SALESDB_H
#define SALESDB_H

#include "Seller.h"

class SalesDB
  {
  // Data members and method prototypes for the SalesDB class go here
  private:
    char salesPerson[30];
    int numSellers;
  public:
    SalesDB();
    SalesDB(const char*);
    void print();
  };

   #endif

销售数据库.cpp

#include "SalesDB.h"
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using std::cout;
using std::endl;
using std::ifstream;
using std::ios;
           //default constructor
SalesDB::SalesDB()
{
numSellers = 0;
} 
         //constructor
SalesDB::SalesDB(const char* ptr)
{
 ifstream salesdb;
 salesdb.open("/location of file", ios::binary); //left out location on purpose
 if(!salesdb)
{
   cout<<"Error...";
   exit(1);
}
 else
{
 salesdb.read((char*) this, sizeof(SalesDB));
}
 salesdb.close();
}
     //print function
void SalesDB::print()
{
 for (int i=0; i < numSellers; i++)
  {
   cout << "Sales Database Listing " << i << ": " << endl;
   salesPerson[i].print();
  }
}

卖家.h

 class Seller
      {
        public:
          Seller();
          Seller(char[],double);
          char* getName();
          double getSalesTotal();
          void setSalesTotal(double);
          void print();

        private:
          char name[30]{};
          double salesTotal = 0;
      };

   #endif

卖家.cpp

#include "Seller.h"
#include <iomanip>
#include <iostream>
#include <cstring>

using std::cout;
using std::left;
using std::right;
using std::endl;
using std::setw;
using std::fixed;
using std::setprecision;

    //default constructor
Seller::Seller()
{
name[0]='\0';
}

    //Constructor
Seller::Seller(char newName[], double newTotal)
{
strcpy(name,newName);
salesTotal=newTotal;
}

   //getName
char* Seller::getName()
{
return name;
}

对于我为什么会收到此错误以及如何修复它的任何帮助,我们将不胜感激。

最佳答案

salesPersonchar 类型的数组,因此没有 print() 成员。试试看:

Seller salesPerson[30];  // was char salesPerson[30]

关于c++ - 非类类型成员打印错误请求 'char',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723432/

相关文章:

c++ - C++中动态分配内存的碎片整理

javascript - 通过 JS 获取 Select 选项值并设置数组索引

arrays - OCaml:动态数组?

c++ - 如何获取指针指向的变量的类型?

c++ - 将函数作为模板参数传递

c++ - for循环重用变量的位置

c++ - 为什么这个 float union 的输出是 NaN?

javascript - 在 JS 对象数组中,我应该使用键来标识每个对象吗?

c - 为什么 char* 可以在 C 中保存单个 char?

c - 如何将结构的内容保存到 C 文件中?