Referencia indefinida al archivo

I'm trying to link/reference separate files together for compilation. I've never done this before, so I'm lost with this error. It parece like I've referenced what I need.

I've got three files, main.cpp, parser.cpp and header.h Main calls parser to parse text (although I haven't been able to test if it actually works yet :l )

principal.cpp -

#include "header.h"

using namespace std; // I know this isn't recommended, I'm changing this later.

        int main(){
    string input, arg1, arg2;
    vector<string> parsedIn;
    cout << "<";
    while(getline(cin, input)){


            parsedIn = parser(input);
            //more code, this is the only call to parser and compile error 
            //stops here

parser.cpp -

#include "header.h"

std::vector<std::string> parser(std::string &input){
int i=0;
//int begin=1;
int  count=0;
std::vector<std::string> parsedIn;

        while(i<input.length()){

                char temp = input.at(i);
                if(temp != ' '){
                parsedIn[count] += temp;
                count++;
                }
        i++;
        }

        if(count < 3)
        parsedIn[0] = "Error"; // Set first value to "Error" to report an input issue

return parsedIn;
}

encabezado.h

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

std::vector<std::string> parser(std::string &input);

I know I should be using guards as well, but my TA wasn't exactly clear on how I set those up...baby steps though. This is my first time with C++ so I'd like to figure out why this isn't being referenced.

The error is undefined reference to parser, of course.

Edit: I made changes to the code to reflect what I've done at your suggestion.

Specifically parse(std::string &input) has become parser(std::string &input)

preguntado el 09 de septiembre de 13 a las 23:09

Paste the commands you are using to attempt to compile and/or link. -

What command do you execute immediately before you get the error? Hit the compike button/gcc/whatever... -

Is this code the same you are compiling? You have a function called parser in header.h, and another called parse in parser.cpp. -

To compile, I've been doing g++ main.cpp I didn't think it should be any different than normal. -

@Clark Please post the exact error message. That would be helpful. -

3 Respuestas

parser.cpp has the "parser" method spelled "parse", but your header.h says it's named "parser".

Respondido el 09 de Septiembre de 13 a las 23:09

Wow that was really obvious. I changed it, but I'm still getting the same compile error. So is there something else? I also made the changes Mahesh pointed out below with vectors. - user2745184

@clark My changes has nothing to do with the compilation issue. Accessing invalid indexes is a run-time issue. - Mahesh

You have another problem.

std::vector<std::string> parsedIn;
// ...
parsedIn[count] += temp;

Size of the vector parsedIn in 0. Using [] on an empty vector causes undefined behavior. You need to perform hacer retroceder operations to add elements to the vector. Unlike std::map container, you can not use [] operator to add elements to the vector.

Respondido el 11 de Septiembre de 13 a las 13:09

Do you mean "Unlike std::map... "? - Pato morando

@MooingDuck If you see my last-1 edit, it was "Unlike std::map". I felt the meaning would turn other wise if I used "Unlike". I think you are correct. Still struggling with English :P - Mahesh

Is it okay to reference non-empty vectors like this? Say I have a vector with 3 items inside, could I evaluate the contents with something like 'if(parsedIn[1] == "foo")' ? - user2745184

Yes. You can do that as long as the vector has elements. But make sure that you are not crossing vector's bounds. Meaning if the vector has 3 elements, you can only access index 0,1,2. Any other index is not a valid index. - Mahesh

You are failing to aquí tus archivos

Small steps:

1) escribir helloWorld if you haven't already. The source file can be called hello.cpp.

2) Instead of building in one step from source to executable, like this:

g++ hello.cpp

try building the archivo objeto, hello.o, then building the executable out of that:

g++ -c hello.cpp -o hello.o
g++ hello.o -o hello

3) Add some little function, call it void foo(), that takes no arguments, returns nothing, prints out something and does nothing else. Put it in hello.cpp y do not proceed until it works perfectly.

4) Mover foo() to its own source file (foo.cpp) and header file (foo.h). Don't worry about header guards for now. Compile and build like this:

g++ -c hello.cpp -o hello.o
g++ -c foo.cpp -o foo.o
g++ hello.o foo.o -o hello

If you get this far, you've done pretty well.

Respondido el 10 de Septiembre de 13 a las 02:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.