C++ 11未定义的函数引用

Gab*_*l.R 0 c++ compiler-errors function c++11

我无法找到解决问题的方法,我认为它与重载函数有关,但我似乎无法弄清楚如何解决它.

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的function.h

#ifndef FN_H
#define FN_H
#include <iostream>

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

#endif
Run Code Online (Sandbox Code Playgroud)

我的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;
}
Run Code Online (Sandbox Code Playgroud)

最后是我的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
Run Code Online (Sandbox Code Playgroud)

和我的错误

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
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ara*_*ash 5

CC++区分大小写:

定义:

int Countletter(string sentence, char letter) {
Run Code Online (Sandbox Code Playgroud)

用法:

times = CountLetter(sent, let);
Run Code Online (Sandbox Code Playgroud)

面对链接器错误时,怀疑拼写错误是其中一个场景.

你有他们微妙的区别吗?

Countletter
CountLetter
Run Code Online (Sandbox Code Playgroud)