this is my program i am getting a error while compiling it can someone help me...
error is

----- Build started: Project: paddy, Configuration: Debug Win32 ------
Compiling...
manager.cpp
c:\documents and settings\others\my documents\visual studio 2005\projects\paddy\paddy\manager.cpp(51) : error C2664: 'Pensioner::Pensioner(char *,char *,char *,long,char *)' : cannot convert parameter 1 from 'std::string' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\documents and settings\others\my documents\visual studio 2005\projects\paddy\paddy\manager.cpp(54) : error C2664: 'Customer::Customer(char *,char *,char *,long)' : cannot convert parameter 1 from 'std::string' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Build log was saved at "file://c:\Documents and Settings\others\My Documents\Visual Studio 2005\Projects\paddy\paddy\Debug\BuildLog.htm"
paddy - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


the program is..

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#include "Customer.h"
#include "Pensioner.h"

using namespace std;

class Manager 
{
private:
  void readFile(char const* filename)
  {
     ifstream input(filename);
     if (!input)
     {
       cout << "Error in Opening the file.\n";
       return;
     }

     string line;
     while (getline(input, line))
     {
       istringstream oss(line);
	  

       char cmd;
       oss >> cmd;
 
       switch (cmd)
       {
         case 'A' : {
           if (size == MAX_CUSTOMERS)
           {
             cout << "The store is empty\n";
             return;
           }

           string first, last, phone;
           int Nofcalls;

           oss >> first >> last >> phone >> Nofcalls;
           
           oss >> cmd;
           if (cmd == 'P')
           {
             string pension; 
             oss >> pension;
             customers[size++] = new Pensioner(first, last, phone, Nofcalls, pension);
           }
           else
             customers[size++] = new Customer(first, last, phone, Nofcalls);
           
         
           break;
         }

         case 'D' : {
           if (size == 0)
           {
             cout << "The store is empty\n";
             return;
           }

           delete customers[--size];
         
           break;
         }

         case 'O' : {
           ofstream output("CustomerOutput.dat", ios_base::out | ios_base::app);
           for (int i = 0; i < size; ++i)
             output << *customers[i] << "\n\n";
         
           break;
         }

         default : cout << "Invalid directive\n";
           return;
       }
     }
  }

public:
  Manager()
    : size(0)
  {
  }

  ~Manager()
  {
    for (int i = 0; i < size; ++i)
      delete customers[i];
  }

  void run()
  {
    readFile("telephone.dat");
  }


  enum {MAX_CUSTOMERS = 100};
  int size;
  Customer* customers[MAX_CUSTOMERS];
};

int main()
{
  Manager manager;
  
  manager.run();

  return 0;
}

Recommended Answers

All 3 Replies

The std::string class has no builtin conversion to the char* type: std::string object is not a char array. It has c_str() member function - it returns const char* pointer to the string contents buffer, but don't try to modify the string text directly (that's why it's const pointer).

Why you declare your class constructors with char* parameters? Do you want to change passed char arrays in constructors? You will scarcely do that. So declare these parameters as const char* pointers then add yet another constructor for Pensioner and Customer classes, for example:

class Customer {
public: // parameter names for example only
    Customer(const char* a, const char* b, const char* c, long d);
    Customer(const string& a, const string& b, const string& c, long d);

or use constructors with const char* parameters as

customers[size++] = new Customer(first.c_str(), last.c_str(), phone.c_str(), Nofcalls);

Thanks for that my friend but after using second option it showing

------ Build started: Project: paddy, Configuration: Debug Win32 ------
Compiling...
manager.cpp
c:\documents and settings\others\my documents\visual studio 2005\projects\paddy\paddy\manager.cpp(51) : error C2664: 'Pensioner::Pensioner(char *,char *,char *,long,char *)' : cannot convert parameter 1 from 'const char *' to 'char *'
Conversion loses qualifiers
c:\documents and settings\others\my documents\visual studio 2005\projects\paddy\paddy\manager.cpp(54) : error C2664: 'Customer::Customer(char *,char *,char *,long)' : cannot convert parameter 1 from 'const char *' to 'char *'
Conversion loses qualifiers
Build log was saved at "file://c:\Documents and Settings\others\My Documents\Visual Studio 2005\Projects\paddy\paddy\Debug\BuildLog.htm"
paddy - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Sorry, but you must change constructor parameters type to const char* in the 2nd option too. Of course, you can use const_cast operator (or C-cast) w/o this correction but it's the other story (don't do that)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.