В чем заключается проблема:
Работаю в С++ с классами, создаю ClassName.cpp и ClassName.h Пишу прототипы методов в .h файле, реализую в .срр, но ничего не работает, выдает ошибку:
//моя среда разработки - CLion
"D:\CLion 2016.3.3\bin\cmake\bin\cmake.exe" --build D:\workspaceForCLion\StudyProj\cmake-build-debug --target StudyProj -- -j 2
[ 20%] Linking CXX executable StudyProj.exe
CMakeFiles\StudyProj.dir/objects.a(main.cpp.obj): In function `main':
D:/workspaceForCLion/StudyProj/main.cpp:5: undefined reference to `LinkedList<int>::LinkedList()'
D:/workspaceForCLion/StudyProj/main.cpp:6: undefined reference to `LinkedList<int>::append(int)'
D:/workspaceForCLion/StudyProj/main.cpp:7: undefined reference to `LinkedList<int>::append(int)'
D:/workspaceForCLion/StudyProj/main.cpp:8: undefined reference to `LinkedList<int>::append(int)'
D:/workspaceForCLion/StudyProj/main.cpp:10: undefined reference to `LinkedList<int>::output()'
D:/workspaceForCLion/StudyProj/main.cpp:5: undefined reference to `LinkedList<int>::~LinkedList()'
D:/workspaceForCLion/StudyProj/main.cpp:5: undefined reference to `LinkedList<int>::~LinkedList()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\StudyProj.dir\build.make:173: recipe for target 'StudyProj.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/StudyProj.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/StudyProj.dir/rule' failed
Makefile:117: recipe for target 'StudyProj' failed
mingw32-make.exe[3]: *** [StudyProj.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/StudyProj.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/StudyProj.dir/rule] Error 2
mingw32-make.exe: *** [StudyProj] Error 2
НО, если я реализую в файле main.cpp все методы, а файл "ClassName.cpp" пусть - все отлично работает.
Собственно, в чем вопрос - как реализовать методы в .cpp файле, не мейн, чтобы они работали?
И еще проблема с реализацией перегрузки операции " << ", кто знает, как можно вывести через перегрузку операции линейный список - подскажите. Я пытался реализовать friend функцию, но всегда выдавало ошибки, пришлось выводить через метод "output".
Сам проект:
LinkedList.h
#pragma once
#include <iostream>
using namespace std;
typedef unsigned int ui;
template<class T>
class LinkedList {
private:
struct node {
T data;
node *next;
} *head;
public:
LinkedList();
~LinkedList();
void append(T data);
void prepend(T data);
void remove(T data);
void clear();
void output();
};
LinkedList.cpp
#include "LinkedList.h"
template <class T>
LinkedList<T>::LinkedList() {
head=NULL;
};
template <class T>
LinkedList<T>::~LinkedList() {
node *p, *q;
p = head;
if(p==NULL) return;
while(p){
q = p->next;
delete p;
p = q;
}
}
template <class T>
void LinkedList<T>::prepend(T data){
node *p, *q;
if(head==NULL){
head = new node;
head->data = data;
head->next = NULL;
return;
}
}
template <class T>
void LinkedList<T>::remove(T data){
node *p, *q;
if(head == NULL) return;
p = head;
while(p){
if(p->data == data){
q->next = p->next;
delete p;
return;
}
q = p;
p = p->next;
}
}
template <class T>
void LinkedList<T>::clear(){
node *p, *q;
if(head==NULL) return;
p = head;
while(p){
q = p->next;
delete p;
if(q != head){
head = NULL;
return;
}
p = q;
}
}
template <class T>
void LinkedList<T>::append(T data){
node *p, *q;
if(head==NULL){
head = new node;
head->data = data;
head->next = NULL;
return;
}
p = head;
while(p->next!=NULL)
p = p->next;
q = new node;
q->data = data;
q->next = NULL;
p->next = q;
}
template <class T>
void LinkedList<T>::output() {
node *p;
p = this->head;
while (p != NULL) {
std::cout << p->data << " -> ";
p = p->next;
}
cout << "NULL";
}
main.cpp
#include <conio.h>
#include "LinkedList.h"
int main(int argc, char** argv) {
LinkedList<int> mylist;
mylist.append(1);
mylist.append(2);
mylist.append(3);
mylist.output();
_getch();
return 0;
}
Реализация всех методов в main.cpp
#include <conio.h>
#include "LinkedList.h"
template <class T>
LinkedList<T>::LinkedList() {
head=NULL;
};
template <class T>
LinkedList<T>::~LinkedList() {
node *p, *q;
p = head;
if(p==NULL) return;
while(p){
q = p->next;
delete p;
p = q;
}
}
template <class T>
void LinkedList<T>::prepend(T data){
node *p, *q;
if(head==NULL){
head = new node;
head->data = data;
head->next = NULL;
return;
}
}
template <class T>
void LinkedList<T>::remove(T data){
node *p, *q;
if(head == NULL) return;
p = head;
while(p){
if(p->data == data){
q->next = p->next;
delete p;
return;
}
q = p;
p = p->next;
}
}
template <class T>
void LinkedList<T>::clear(){
node *p, *q;
if(head==NULL) return;
p = head;
while(p){
q = p->next;
delete p;
if(q != head){
head = NULL;
return;
}
p = q;
}
}
template <class T>
void LinkedList<T>::append(T data){
node *p, *q;
if(head==NULL){
head = new node;
head->data = data;
head->next = NULL;
return;
}
p = head;
while(p->next!=NULL)
p = p->next;
q = new node;
q->data = data;
q->next = NULL;
p->next = q;
}
template <class T>
void LinkedList<T>::output() {
node *p;
p = this->head;
while (p != NULL) {
std::cout << p->data << " -> ";
p = p->next;
}
cout << "NULL";
}
int main(int argc, char** argv) {
LinkedList<int> mylist;
mylist.append(1);
mylist.append(2);
mylist.append(3);
mylist.output();
_getch();
return 0;
}
Проблема с перегрузкой "<<":
добавил др функцию:(пространство имен std определено ранее в .h файле, сама функция реализована и объявлена в нем же)
friend ostream& operator<<(ostream& os, LinkedList<T> &s);
template <class T>
ostream& operator<<(ostream& os, LinkedList<T> &s){
if(s.head == NULL){
cout << "List is empty" << endl;
return os;
}
node *p;
p = s.head;
while (p != NULL) {
cout << p->data << " -> ";
p = p->next;
}
cout << "NULL" << endl;
return os;
}
Выдает ошибку
"D:\CLion 2016.3.3\bin\cmake\bin\cmake.exe" --build D:\workspaceForCLion\StudyProj\cmake-build-debug --target StudyProj -- -j 2 [ 50%] Building CXX object CMakeFiles/StudyProj.dir/main.cpp.obj In file included from D:\workspaceForCLion\StudyProj\main.cpp:2:0: D:\workspaceForCLion\StudyProj\LinkedList.h:29:65: warning: friend declaration 'std::ostream& operator<<(std::ostream&, LinkedList&)' declares a non-template function [-Wnon-template-friend] friend ostream& operator<<(ostream& os, LinkedList &s); ^ D:\workspaceForCLion\StudyProj\LinkedList.h:29:65: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) D:\workspaceForCLion\StudyProj\LinkedList.h: In function 'std::ostream& operator<<(std::ostream&, LinkedList&)': D:\workspaceForCLion\StudyProj\LinkedList.h:130:5: error: 'node' was not declared in this scope node *p; ^~~~ D:\workspaceForCLion\StudyProj\LinkedList.h:130:11: error: 'p' was not declared in this scope node p; ^ mingw32-make.exe[3]: [CMakeFiles/StudyProj.dir/main.cpp.obj] Error 1 mingw32-make.exe[2]: [CMakeFiles/StudyProj.dir/all] Error 2 mingw32-make.exe[1]: [CMakeFiles/StudyProj.dir/rule] Error 2 mingw32-make.exe: * [StudyProj] Error 2 CMakeFiles\StudyProj.dir\build.make:61: recipe for target 'CMakeFiles/StudyProj.dir/main.cpp.obj' failed CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/StudyProj.dir/all' failed CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/StudyProj.dir/rule' failed Makefile:117: recipe for target 'StudyProj' failed