Reputation: 43
I want to pass a pointer to function in a class but its showing a mistake and, frankly saying, I don't know why.I have been closely following tutorial, where its written like that, but they don't have problem while I do. Could anyone give some tips? I am relatively new in c++. Thank you in advance.
If that helps, here I am making kind of a dictionary prototype, here I am trying to read from input file pairs of words and save them in linked list as I am required to use it. I am using Code::Blocks program.
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
//-------------------------------
struct node
{
string Language;
string English;
node* next;
};
//-------------------------------
class Dictionary //class where all functions will go
{
public:
void readFirstElement(node *head, node *tail);
void readElements(node* head, node* tail);
// void insertElements();
// void deleteElements(node* &head, node* &tail);
//-------------------------------
void readFirstElement(node *head, node *tail)
{
string word1, word2;
node* temp;
ifstream input;
input.open("input.txt");
input >> word1 >> word2;
temp=new node;
temp->Language=word1;
temp->English=word2;
temp->next=NULL;
cout << temp->Language <<" ir "<< temp->English << endl;
head=temp;
tail=temp;
input.close();
}
//-------------------------------
void readElements(node* &head, node* &tail)
{
string word1, word2;
node* temp;
ifstream input;
input.open("input.txt");
while (!input.eof( ))
{
input >> word1 >> word2;
temp=new node;
temp->Language = word1;
temp->English=word2;
tail->next=temp;
tail=temp;
}
tail->next=NULL;
input.close();
}
int main()
{
node* head = NULL;
node* tail = NULL;
Dictionary ob;
ob.readFirstElement(&head, &tail);
ob.readElements(&head, &tail);
node* temp = head;
return 0;
}
Main mistake it is writing is error: 'void Dictionary::readFirstElement(node*, node*)' cannot be overloaded.
Upvotes: 1
Views: 109
Reputation: 103751
This has nothing to do specifically with passing pointers. You would have this problem no matter the arguments, even with no arguments at all.
There are two ways to define a member function. You can do it inline, entirely in the class body:
class X {
void foo() {
do_something();
}
};
Or you can just put the declaration it in the class body, then put the definition outside.
class X {
void foo();
};
void X::foo() {
do_something();
}
What you've done is declare your functions in the class body, and then defined them separately, also in the class body. The compiler sees this as two different functions. But since they have the same name and argument types, any attempt to call them would be ambiguous, and so is not allowed. Pick one of the options I showed above.
Upvotes: 1
Reputation: 3355
Put the method definitions in the .cpp
implementation file:
Dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
class Dictionary {
public:
void readFirstElement(node* head, node* tail) const;
void readElements(node* head, node* tail) const;
//...
};
#endif
Dictionary.cpp
#include "Dictionary.h"
void Dictionary::readFirstElement(node* head, node* tail) const {
//...
}
void Dictionary::readElements(node* head, node* tail) const {
//...
}
At the moment you are declaring two pairs of methods with the same signature in the Dictionary
class, which is invalid.
Upvotes: 0