0

Сразу опишу проблему: есть учебное задание для реализации String. Я знаю про STL просто задача такая.

String.h

#include <iostream>

class String {
    char* data;
    int l;
public:
    String();

    template<int N>
    String(const char (&str)[N]); //проблема здесь

    String(const String& other);
    char* get_data() const;
    int get_length() const;

    ~String();
};

String.cpp

#include <stdio.h>
#include "String.h"

String::String(){
    l = 0;
}

template<int N>
String::String(const char (&str)[N]){ // проблема здесь
    l = N;
    data = new char[l];
    std::copy(str, str+l, this->data);    
}

String::String(const String& other){
    l = other.get_length();
    data = new char[l];
    std::copy(other.get_data(), other.get_data()+l, this->data);
}

char* String::get_data() const{
    char* data_copy = new char [this->l];
    std::copy(this->data, this->data+l, data_copy);
    return data_copy;
}

int String::get_length() const{
    return l;
}

String::~String(){
    delete [] data;
}

main.cpp

#include <iostream>
#include "String.h"

int main(int argc, const char * argv[]) {
    String s("123"); // проблема здесь
}

ошибка такая :

Undefined symbols for architecture x86_64:
  "String::String<4>(char const (&) [4])", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Я почти уверен что я не правильно использую шаблон и где-то что-то упустил. Если нужна будет какая-то дополнительная информация - пишите. Я напишу! Заранее всем спасибо за ответ!

Harry
  • 221,325

0 Answers0