user1781966
user1781966

Reputation:

calling functions in C++

Im trying to call a function in C++ and I thought it would be the same as in C, but when trying to convert a C program to C++ I've run into an error where it says functions are undeclared.

Here is my class:

class contacts
 {
  private:;
          char *First_Name;
          char *Last_Name;
          char *home;
          char *cell;
  public:;
  //constructor
         contacts()
         {
         }  
//Function declaration     
void readfile (contacts*friends ,int* counter, int i,char buffer[],FILE*read,char user_entry3[]);

  };

Here is a snippit of my Menu function:

 if(user_entry1==1)
  {
    printf("Please enter a file name");
    scanf("%s",user_entry3); 
    read=fopen(user_entry3,"r+");

   //This is the "undeclared" function
   readfile(friends ,counter,i,buffer,read,user_entry3);
   }else;

I'm obviously doing something wrong, but every time I try and compile I get readfile undeclared(first use this function) What am I doing wrong here?

Upvotes: 1

Views: 226

Answers (4)

Jeff D.
Jeff D.

Reputation: 328

Since your readfile function is inside the contacts class, then the answer above is technically correct in that you need to create an instance of the object then call the function. However, from an OO perspective, your class function should in general only operate on members of the objects of the class that contains it. What you have here is more like a general purpose function which takes parameters of many types and only one of them is a pointer to the class that itself contains the function you are calling which if you think about it is kind of strange. So you would be passing a pointer to the class to a member function of the class which would require two instances of it. You cannot think of a class as a simple substitution for a pointer to a structure. Since this is a class, you declare all the variables you need as members of the class so there is no need to pass them as parameters (one of the main points of a class is to isolate general data from class member data). Here is an update that should point you in the right direction.

   class contacts
     {
      private:

          char *First_Name;
          char *Last_Name;
          char *home;
          char *cell;
          FILE *read;  // these all could also be declared as a stack variable in 'readfile'


    public:
    //constructor

    contacts()
        {
        }  

    //destruction
    ~contacts()
    {
    }

    //Function declaration     
    int contacts::readfile (char * userEnteredFilename);

    };


    contacts myContact = new contacts();

    printf("Please enter a file name");
    scanf("%s",user_entry3); 

    int iCount = myContact->readfile(user_entry3);

    // the readfile function should contain all of the file i/O code 

Upvotes: 0

bartonm
bartonm

Reputation: 1650

I recommend refactoring to use the STL vector.

#include <vector>
#include <ReaderUtil>

using namespace std;

vector< contact > myContactCollection;
myContactCollection.push_back( Contact("Bob",....) );
myContactCollection.push_back( Contact("Jack",....) );
myContactCollection.push_back( Contact("Jill",....) );

Or...

myContactCollection = ReaderClass::csvparser(myFile);

where

ReaderClass::csvparser(std::string myFile) returns vector<Contact>

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

Is the "Menu" function from inside the class contacts? The way you have designed it, it can be only called on an instance of the the class. You have options based on exactly what readfile means to contacts

Im guessing the function reads all contacts and not just 1 contact, which means it can be made a static function

static void readfile(... ;

And call as

contacts::readfile(...;

Alternatively if you dont need direct access to internals of the class you can just declare it outside the class (as a free function, similar to normal C functions) and use exactly as you are doing now. This is in fact what the compiler is searching for when it comes across your code.

Also, I suggest you rename class contacts -> class contact since it appears that objects each holds the contact information of only 1 person.

Upvotes: 0

Naveen
Naveen

Reputation: 73433

You need to create an object of contacts class and then call readfile on that object. Like this: contacts c; c.readfile();.

Upvotes: 2

Related Questions