-2

I am reading in a text file with various int, string and double fields which are tab delimited. I am trying to produce an array of positions of the '/t' characters. I am at my wits end trying to get this to work. I keep getting

undefined reference to getTAB(std::string, int*)

error in main.

What am I doing wrong?

My files are as follows

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "getTAB.h"
using namespace std;
int main ()
{
    int tab = 7; int tabPosition[tab];
    string lineOfInput;
    ifstream inFile("DATA/inputData.txt");
    while(getline(inFile,lineOfInput))
    {
        cout << lineOfInput << endl;
        getTAB(lineOfInput, &tabPosition[0]);   
    }   
    inFile.close();   
return 0; 
}

getTAB.h

#ifndef GETTAB_H_INCLUDED
#define GETTAB_H_INCLUDED
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getTAB(string line, int *array);
#endif // GETTAB_H_INCLUDED

and getTAB.cpp

#include "getTAB.h"
void getTAB(string line, int *array)
{   
    int tab = 0;   
    for(int i=0; i < line.length(); i++)
    {
        if(line.at(i) == '\t')
        {
            array[tab] = i;
            cout << tab <<"----"<< i << endl;
            j++;
        }
    }   
    return; 
}

inputData.txt

1   OTTO BROTHERS   FHS 517267  1417.174    ZA 11 TRPF 18162    BC  H
Saksham
  • 9,037
  • 7
  • 45
  • 73
Adam
  • 1
  • Don't use blockquotes, use code blocks. View the preview before posting to prevent such errors. – keyser Aug 18 '13 at 12:02
  • 1
    The problem lies within the fact, that probably result of compiling getTAB.cpp is not linked together main.cpp compilation result. What IDE or compilation command are you using? – BartoszKP Aug 18 '13 at 12:03
  • Besides pulling `j` out of thin air in getTAB.cpp, the code looks ok to compile. I suspect you're just not linking with both `cpp` files, easiest accomplished (from the command line) by compiling them at the same time. – Joachim Isaksson Aug 18 '13 at 12:07
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chris Aug 18 '13 at 12:10

1 Answers1

2

I don't know what command you are using to compile your program, but you should use something like this (in case of g++)

g++ main.cpp getTAB.cpp

and not

g++ main.cpp

You need to explicitly state both files in order to link them together.

I don't know what compiler you are using, though the syntax will remain pretty much the same.

BitParser
  • 3,748
  • 26
  • 42
  • Hi TGO, I am using Code::Blocks and the files (.cpp and .h) were created in the IDE. When compling would I have to somehow manually list the .cpp files. Can you advise how to do this. – Adam Aug 18 '13 at 14:43
  • Hey Adam. Then what you should do is create a new project and add both of your files to the project. Ah, now I noticed that you've already sorted it out as I am on mobile – BitParser Aug 18 '13 at 16:03