Код: Выделить всё
Foo.h
Код: Выделить всё
#pragma once
#include
#include
class Foo {
public:
template
requires std::convertible_to
explicit Foo(String&& s);
private:
std::string m_s;
};
< /code>
[list]
[*]Foo.cppКод: Выделить всё
#include "Foo.h"
template
requires std::convertible_to
Foo::Foo(String&& s) : m_s{std::forward(s)} {}
template Foo::Foo(const std::string& s);
template Foo::Foo(std::string&& s);
< /code>
[list]
[*]test.cppКод: Выделить всё
#include "Foo.h"
int main() {
Foo f1{"test"};
std::string test = "test";
Foo f2{test};
return 0;
}
< /code>
Код компилируется, но не может ссылаться со следующей ошибкой: < /p>
[main] Building folder: /home/bobeff/projects/cpp/test/build/Debug
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/bobeff/projects/cpp/test/build/Debug --config Debug --target all --
[build] [2/3 33% :: 0.149] Building CXX object CMakeFiles/test.dir/test.cpp.o
[build] [2/3 66% :: 0.156] Building CXX object CMakeFiles/test.dir/Foo.cpp.o
[build] [3/3 100% :: 0.188] Linking CXX executable test
[build] FAILED: test
[build] : && /usr/bin/g++ -g CMakeFiles/test.dir/test.cpp.o CMakeFiles/test.dir/Foo.cpp.o -o test && :
[build] /usr/bin/ld: CMakeFiles/test.dir/test.cpp.o: in function `main':
[build] /home/bobeff/projects/cpp/test/test.cpp:4:(.text+0x31): undefined reference to `Foo::Foo(char const (&) [5])'
[build] /usr/bin/ld: /home/bobeff/projects/cpp/test/test.cpp:6:(.text+0x72): undefined reference to `Foo::Foo(std::__cxx11::basic_string&)'
[build] collect2: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: /usr/bin/cmake --build /home/bobeff/projects/cpp/test/build/Debug --config Debug --target all -- exited with code: 1
[driver] Build completed: 00:00:00.220
[build] Build finished with exit code 1
Подробнее здесь: https://stackoverflow.com/questions/796 ... uctor-in-c