C++, undefined reference to the function

I encountered the following error when I tried to compile the program according to the “Functions/8-Organizing Functions in Files” episode:

undefined reference to greet(std::__cxx11::basic_string<char, std::char_traits, std::allocator >)'`

I am using C++ 20, and GCC 10.3.0.

The program includes the following files:

main.cpp

#include <iostream>
#include "utils/greet.h"

using namespace std;

int main() {
    string name = "Mike";
    greet(name);
    return 0;
    }

utils/greet.cpp

#include <iostream>

using namespace std;

void greet(string& name){
    cout << "Hello " << name;
}

utils/greet.h

#ifndef HELLOWORLD_GREET_H
#define HELLOWORLD_GREET_H

#include <string>

void greet(std::string name);

#endif //HELLOWORLD_GREET_H

How did you do your compilation? I think you need to explicitly compile / link all of the relevant files in order to make this work. So something like this:

g++ *.cpp utils/*.cpp -o output
./output

You have a & on the greet(string name) {}

I had the same issue, idk why it does not work. I have opened another topic of it.

Looking at the header again I wonder:

void greet(std::string name);

Probably needs to have the & also:

void greet(std::string& name);

If I put the & on both, I get an error “C2644, void greet(std::string &) cannot convert argument 1 from const char to std::string &”

I do not have char type anywhere… but I think it is thinking & is the char in question, it does not say the entire argument: (std::string& name) it just says sdt::string &, which resulted me thinking & is incompatible with the separate files.

Can you paste your exact code? Ideally in code blocks:

```cpp
// C++ code here
```

Sorry for replying to an old question.

I think you have not included your header file (greet.h) in your implementation file (greet.cpp).

Also, you can use ostream in greet.cpp, as you are using only cout.