0

When I compile my program, I get three "undefined reference to 'PrintArgv(...)'" errors. I have searched but I cannot find the reason why I am getting these errors. Here is my code:

    #include "tools.hpp"

enum ARG_T {COMMAND, SWITCH, ARGUMENT};
void ReadArgs(int, char**, ofstream&);
void PrintArgv(char*, ARG_T, ofstream&);


int main(int argc, char* argv[])
{
    ofstream fout;
    try{
        fout.open("P1Ford.txt", ios::out | ios::app);
    }
    catch(...)
    {
        //fatal("Could not open file P1Ford.txt");
        cout << "Could not open file P1Ford.txt" << "\n";
        return -1;
    }

    ReadArgs(argc, argv, fout);

    fout.close();
    return 0;
}

/*-----------------------------------------------------------------------------
This function takes the values passed in through the command line
then examines each one to determine if it is the command, a switch or
an argument. Then passes it to a function to print it to the file.
-----------------------------------------------------------------------------*/
void ReadArgs(int argc, char* argv[], ofstream& fout)
{
    for(int c=0; c<argc; ++c)
    {
        if(c == 0) PrintArgv(argv[c], COMMAND, fout);
        else if(strncmp(argv[c], "-", 1) == 0) PrintArgv(argv[c], SWITCH, fout);
        else if(strncmp(argv[c], ">", 1) == 0) ;
        else PrintArgv(argv[c], ARGUMENT, fout);
    }
}

/*-----------------------------------------------------------------------------
This function prints the argument in the correct format
Command <command>
   Switch <switch>
   Argument <agrument>
   Argument <argument>
-----------------------------------------------------------------------------*/
void PrintArgv(char* val, ARG_T type, ostream& fout)
{
    if(type == COMMAND) fout << "Command " << val << "\n";
    if(type == SWITCH) fout << "\t" << "Switch " << val << "\n";
    if(type == ARGUMENT) fout << "\t" << "Argument " << val << "\n";
}

The function ReadArgs is where I am getting the errors. Each call to PrintArgv gives me the error. I am using G++ 4.6.1.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
DFord
  • 2,352
  • 4
  • 33
  • 51

2 Answers2

5

Your prototype has a parameter of type ofstream & and your header has one of type ostream &. This means that the implementation of PrintArgv is actually declaring a new function. You call the one you specified a prototype for, though, which has no body because you inadvertently created a new function instead of defining the body of the original one.

chris
  • 60,560
  • 13
  • 143
  • 205
  • Wow, thank you. I checked the prototype and the call but did not double check the function itself. – DFord Apr 07 '12 at 01:43
  • Just remember that the error you're getting occurs when a function is declared, but has no executable body that the linker can find. This will definitely help debugging future code. – chris Apr 07 '12 at 01:45
1

Replace your function definition at the top by the arguments in fact used:

#include "tools.hpp"

enum ARG_T {COMMAND, SWITCH, ARGUMENT};

void ReadArgs(int argc, char* argv[], ofstream& fout)
void PrintArgv(char* val, ARG_T type, ostream& fout)
Haatschii
  • 9,021
  • 10
  • 58
  • 95
  • On the surface this looks like he's suggesting adding the named parameters instead of just the types. But it looks like he did catch the ofstream/ostream difference in the PrintArgv() proto. +1 – Amardeep AC9MF Apr 07 '12 at 01:49