Здравствуйте, решая задачу на наследование, столкнулся с такой проблемой: компилятор выдает ошибку, когда я определяю конструктор унаследованного класса. Совсем не могу понять, что я делаю не так, хотя перечитал главу в книге тщательно и вроде задача несложная, но как только этот конструктор появляется в файле с определением методов этого класса и базового, то появляются проблемы. Я работаю в Visual Studio 2010.
Вот код:
// Заголовочный файл
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include <string>
class Cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(char *, char *, int, double);
Cd(const Cd &);
Cd() { performers[0] = '\0'; label[0] = '\0'; selections = 0; playtime = 0.0; }
virtual ~Cd();
virtual void Report() const;
Cd & operator= (const Cd &);
};
class Classic : public Cd
{
private:
std::string primWork;
public:
Classic(const char * s1, const char * s2, int n, double x, const std::string & s3);
Classic(const Cd &, const std::string &);
Classic();
~Classic();
virtual void Report() const;
};
#endif
// Определение методов
#include <iostream>
#include "classic.h"
Cd::Cd(const char * s1,const char * s2, int n, double x)
{
strncpy(performers, s1, 49);
strncpy(label, s2, 19);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & c)
{
strcpy(performers, c.performers);
strcpy(label, c.label);
selections = c.selections;
playtime = c.playtime;
}
Cd & Cd::operator=(const Cd & c)
{
if (this == &c)
return *this;
strcpy(performers, c.performers);
strcpy(label, c.label);
selections = c.selections;
playtime = c.playtime;
return *this;
}
void Cd::Report() const
{
std::cout << "Performers: " << performers << "; Label: " << label << "; Selections: " << selections << "; Playtime: " << playtime << std::endl;
}
Cd::~Cd()
{
}
Classic::Classic(const char * s1, const char * s2, int n, double x, const std::string & s3) : Cd(s1, s2, n, x), primWork(s3)
{
}
// Программу для тестирование еще даже не написал
#include <iostream>
using namespace std;
#include "classic.h"
int main()
{
return 0;
}
Стоит мне удалить конструктор:
Classic::Classic(const char * s1, const char * s2, int n, double x, const std::string & s3) : Cd(s1, s2, n, x), primWork(s3)
{
}
Консольное окно выводится без всяких дополнительных сообщений и написано Build Succeeded. Добавляю конструктор и пишет Build Failed.
Вот весь текст, что написан компилятором:
1>------ Build started: Project: cd, Configuration: Debug Win32 ------
1>Build started 15.08.2017 14:49:25.
1>InitializeBuildStatus:
1> Touching "Debug\cd.unsuccessfulbuild".
1>ClCompile:
1> Methods.cpp
1>c:\users\константин\documents\visual studio 2010\projects\cd\cd\methods.cpp(6): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\константин\documents\visual studio 2010\projects\cd\cd\methods.cpp(7): warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(188) : see declaration of 'strncpy'
1>c:\users\константин\documents\visual studio 2010\projects\cd\cd\methods.cpp(14): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
1>c:\users\константин\documents\visual studio 2010\projects\cd\cd\methods.cpp(15): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
1>c:\users\константин\documents\visual studio 2010\projects\cd\cd\methods.cpp(24): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
1>c:\users\константин\documents\visual studio 2010\projects\cd\cd\methods.cpp(25): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
1> Generating Code...
1> Compiling...
1> test.cpp
1> Generating Code...
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>Methods.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Classic::Report(void)const " (?Report@Classic@@UBEXXZ)
1>Methods.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Classic::~Classic(void)" (??1Classic@@UAE@XZ) referenced in function "public: virtual void * __thiscall Classic::`scalar deleting destructor'(unsigned int)" (??_GClassic@@UAEPAXI@Z)
1>C:\Users\Константин\documents\visual studio 2010\Projects\cd\Debug\cd.exe : fatal error LNK1120: 2 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.40
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Так происходит с любой задачей на наследование. Сам разобраться в этом не смог...