vendredi 2 mars 2018

C++11 Undefined reference to function

I unable to find a solution to my problem, I think it has something to do with overloading functions but I can't seem to figure out how to resolve it.

here is my function.cpp

#include "CountLetter.h"
int Countletter(string sentence, char letter) {
    int size = sentence.length();
    int toReturn = 0;

    for (int i = 0; i<= size; ++i) {
            if (sentence[i] == letter) {
                    toReturn++;
            }
    }
    return toReturn;
}

Here is my function.h

#ifndef FN_H
#define FN_H
#include <iostream>

using namespace std;
int CountLetter(string sentence, char letter);

#endif

My main.cpp

#include "CountLetter.h"

int main() {
    string sent = "";
    char let = ' ';
    int times = 0;

    cout << "Enter a sentence.\n";
    getline(cin, sent);
    cout << "Enter a letter.\n";
    cin >> let;
    times = CountLetter(sent, let);
    cout << "The letter " << let << " occurred " << times << " time(s).\n";

    return 0;
}

and finally my makefile

lab16: lab16.o CountLetter.o
    g++ -std=c++11 -o lab16 lab16.o CountLetter.o
lab16.o: lab16.cpp
    g++ -std=c++11 -o lab16.o -c lab16.cpp
CountLetter.o: CountLetter.h CountLetter.cpp
    g++ -std=c++11 -o CountLetter.o -c CountLetter.cpp

and my errors

lab16.o: In function `main':
lab16.cpp:(.text+0xb4): undefined reference to 
`CountLetter(std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, char)'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'lab16' failed
make: *** [lab16] Error 1

Thanks!

Aucun commentaire:

Enregistrer un commentaire