gives me error

main

/* 
The purpose of this lab is to open a text file and then make 
a copy of it
*/


#include <iostream>
#include <fstream>
#include "Interface.h"
using namespace std;

int main()
{
//open up file to be read from
    string input;
    ifstream in;
    cout<<"enter file name which needs to be opened\n";
    cin>>input;
    in.open(input.c_str());
    while(!in)
    {
        in.close();
        cout<<"the input textfile failed please reenter the file\n";
        in.clear();
        cin>>input;
        in.open(input.c_str());
    }
    cout<<"the file opened\n";
    //open up file to copy to
    string output;
    ofstream out;
    cout<<"enter file name which to copy to\n";
    cin>>output;
    out.open(output.c_str());
    while(!out)
    {
        out.close();
        cout<<"output file opening failed. Please reenter output file name\n";
        out.clear();
        cin>>output;
        out.open(output.c_str());
    }
    copyname a;
//read in data
    a.readinput(in);
//   use f.getline for Cstring and >> for rest...
    return 0;
}

interface

#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
#include <iostream>
using namespace std;
class copyname
{
    public:
    void readinput(ifstream& input);

};

#endif // INTERFACE_H_INCLUDED

implementation

#include "Interface.h"
#include <fstream>

void readinput(ifstream& input)
{
    string name;
    getline(input,name);
    cout<<name; 
}

error in main not sure why though i declare a class than try to call the function.

Recommended Answers

All 15 Replies

doesn't like a.readinput(in)

readinput() is a method of class.

void copyname::readinput(ifstream& input)
{
    string name;
    getline(input,name);
    cout<<name; 
}

it works if i have it in my int main so i must be doing something wrong with the implementation and interface files.

thanks for the help.

still gives me same error. but works if i have it in int main()

lotrsimp12345,

still gives me same error. but works if i have it in int main

Post error description or message.

undefiined reference to 'copyname::readinput(std::basic_ifstream<char,std::char_traits...'

Include fstream header file.

already have
here is modified code
main

/* 
The purpose of this lab is to open a text file and then make 
a copy of it reading in name, id number, gpa and grade
*/


#include <iostream>
#include <fstream>
#include "Interface.h"
using namespace std;

int main()
{
//open up file to be read from
    string input;
    ifstream in;
    cout<<"enter file name which needs to be openedn";
    cin>>input;
    in.open(input.c_str());
    while(!in)
    {
        in.close();
        cout<<"the input textfile failed please reenter the filen";
        in.clear();
        cin>>input;
        in.open(input.c_str());
    }
    cout<<"the file openedn";
    //open up file to copy to
    string output;
    ofstream out;
    cout<<"enter file name which to copy ton";
    cin>>output;
    out.open(output.c_str());
    while(!out)
    {
        out.close();
        cout<<"output file opening failed. Please reenter output file namen";
        out.clear();
        cin>>output;
        out.open(output.c_str());
    }

    copyname a;
    //read in data
        a.readinput(in);
    //
    return 0;
}

interface

#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
#include <iostream>
#include <fstream>
using namespace std;
class copyname
{
    public:
    void readinput(ifstream& inputfile);
};

#endif // INTERFACE_H_INCLUDED

implementation

#include "Interface.h"
#include <fstream>
using namespace std;
void copyname::readinput(ifstream& inputfile)
{
    string name;
    getline(inputfile,name);
    cout<<name;
}

thanks for help. I will just ask teacher tom.

I am sorry. I missed this :

undefiined reference to 'copyname::readinput(std::basic_ifstream<char,std::char_traits...'

It's linker error - readinput().

Which compiler are you using?

all my files end in the correct ending should i rename the file?

codeblocks

1. Select Project Menu + Properties + Build Targets Tab
2. Checked all Build Target files. (three files - interface.h, .cpp ...).

thanks it works now!! wasted 4 hours on this. :(.

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.