why do i get undefined reference to error in my main with regards to isvalidname. It says undefined reference to...


in my isvalidname i check if they enter in first name, char ws and then last name. Then i want to print out everything that i have deleted on that line. i guess i could use getback() or putback() or store my string at the beginning.

MAIN

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

int main()
{
    //open up file to be read 
    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());
    }
    //processing of file
    line_test a;
    out<<a.getfirst3(in);
    
    //string inputline;
    
    while(!in.eof())
    {
        a.iscomment(in,out);
        a.islabel(in,out);
        a.isequal(in,out);
        a.isvalidname(in,out);
        /*
        getline(in,inputline);
        a.iscomment(in);
        a.islabel(in);
        a.isequal(in);
        a.is_space();
        a.isvalidname(in);*/
    }
    in.close();
    out.close();
    return 0;

}

INTERFACE

#ifndef LINE_TEST_H_INCLUDED
#define LINE_TEST_H_INCLUDED
#include <fstream>
#include <iostream>
using namespace std;
class line_test
{
    public:
    //void iscomment(ifstream& input,ofstream& output);
    //string inputline;
    //string inputline1;
    //void islabel(ifstream& input,ofstream& output);
    
    
    //first line of file
    string title_comments;
    //second line of file
    string title_comments1;
    //third line of file
    string title_comments2;
    //first three lines of code
    string first_three_lines;
    string getfirst3(ifstream& input);
    
    void iscomment(ifstream& input,ofstream& output);
    void islabel(ifstream& input,ofstream& output);
    void isequal(ifstream& input,ofstream& output);
    void isvalidname(ifstream& input,ofstream& output);
    //void is_space();    
    //every line other than comment lines
    string inputline;
    string inputline3;
    string inputline2;
    //store everything that except for label in this string
    //string inputline2;
    //
    //string inputline3;
    //used to see if finds label or not
    
    //void isequal(ifstream& input);
    //string isvalid_entry(ifstream& input);*/
};



#endif // LINE_TEST_H_INCLUDED

IMPLEMENTATION

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

//retrieve first 3 lines
string line_test::getfirst3(ifstream& input)
{
    string newline="\n";
    getline(input,title_comments);
    getline(input,title_comments1);
    getline(input,title_comments2);
    title_comments=title_comments+newline;
    title_comments1=title_comments1+newline;
    title_comments2=title_comments2+newline;
    first_three_lines=title_comments+title_comments1+title_comments2;
    return first_three_lines;
}
/*
void line_test::iscomment(ifstream& input,ofstream& output)
{
    if(input.peek()=='#')
    {
        getline(input,inputline);
        int length=inputline.length();
        input.ignore(0,length);
        output<<"";
    }
    else
    {
        getline(input,inputline);
        //output<<inputline<<"\n";
    }
}

void islabel(ifstream& input,ofstream& output)
{
    //string inputline1;
    if(input.peek('n'))
    {
        //getline(input,inputline1);
        input.ignore(0,4);
        output<<inputline;
    }
    else
    {
        output<<"nothing";
    }
}

*/


//function used to check if there are comments

void line_test::iscomment(ifstream& input,ofstream& output)
{
    getline(input,inputline);
    if(inputline.find('#')==0)
    {
        int length=inputline.length();
        inputline.erase(0,length);
        input.ignore(0,length);
        output<<"";
    }
    else
    {
        //output<<inputline<<"\n";
    }
}

//checks if data is labeled correctly
void line_test::islabel(ifstream& input,ofstream& output)
{
    if(inputline.find("name")==0)
    {
        inputline.erase(0,4);
        input.ignore(0,4);
        //output<<inputline<<"\n";
        //cout<<"name"<<inputline2<<"\n";
    }
    
    else if(inputline.find("ID")==0)
    {
        inputline.erase(0,2);
        input.ignore(0,2);
        //output<<inputline<<"\n";
        //cout<<"id"<<inputline2<<"\n";
    }
    else if(inputline.find("GPA")==0)
    {
        inputline.erase(0,3);
        input.ignore(0,3);
        //output<<inputline<<"\n";
        //cout<<"gpa"<<inputline2<<"\n";
    }
    else if(inputline.find("gender")==0)
    {
        inputline.erase(0,6);
        input.ignore(0,6);
        //output<<inputline<<"\n";
        //cout<<"gender"<<inputline2<<"\n";
    }
    else
    {
        int length=inputline.length();
        inputline.erase(0,length);
        input.ignore(0,length);
        //output<<"";
    }
}

//check if their an equal sign 
void line_test::isequal(ifstream& input,ofstream& output)
{

    while(inputline.find(' ')==0||inputline.find('=')==0) //I changed inputline2 to inputline and it worked fine.
    {
            //output<<"input is"<<inputline2<<"\n";
            
            inputline.erase(0,1);
            input.ignore(0,1);
    }
    //output<<inputline<<"\n";
}
void isvalidname(ifstream& input,ofstream& output)
{
    
    string first_name;
    char ws;
    string last_name;
    if(input>>first_name>>ws>>last_name)
    {
        output<<first_name<<ws<<last_name;
    }
}

Dani AI

Generated

The undefined-reference error is a linker problem: the header declares a member function but the implementation file does not define that member (it defines a free function instead). The fix is to implement the function with the class scope so the linker finds the symbol declared in the header. For example:

void line_test::isvalidname(std::ifstream& in, std::ofstream& out)
{
    std::string first, last;
    if (in >> first >> last) {
        out << first << ' ' << last << '\n';
    }
}

Parsing name tokens: reading a char with the formatted extraction operator (>>) does not capture separating whitespace (it skips whitespace), so a construct that tries to read a space with >> will instead get the first character of the next token. To capture the actual separator or the remainder of the line use one of these techniques: (a) read two tokens (first and last) with >> if last names have no spaces; (b) read the rest of the line with std::getline and parse it with an istringstream; or (c) use istream::get() to read a single character including whitespace. The get and ignore behavior is documented on cppreference (see basic_istream::get and basic_istream::ignore).

Also avoid loop and I/O anti-patterns seen in the thread: prefer while (std::getline(in, line)) or while (in >> token) over while (!in.eof()). To discard the remainder of a line reliably use in.ignore(std::numeric_limits<std::streamsize>::max(), '\n') rather than passing minimal or incorrect counts to ignore.

’s follow-up (“nvm”) likely reflects discovering the missing class scope. Additional checks: confirm signatures match (const/ref qualifiers), recompile all translation units, and turn on linker diagnostics if the undefined reference persists.

nvm i think i know what the problem is.

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.