Hello!
I study c ++, work environment VS 2010 SP 1. There was an error with redefinition, I can not understand why.
#ifndef STRING_H #define STRING_H class String { public: String(const char *str=""); ~String(); int Len() const {return n;} void Print() const ; private: char *s; int n; }; #endif 2 file:
#include <iostream> #include <cstring> #include "String.h" using namespace std; String ::String(const char *str="") { n = strlen(str); s = new char[n+1]; //здесь д.б. обработка ошибка strcpy(s,str); } String :: ~String() { delete[] s; } void String :: Print() const { cout<<s; } 3 file:
#include <iostream> #include "String.h" using namespace std; int main() { String a("Hello, world!"); cout << "String a: \""; a.Print(); cout << "\", Len = " << a.Len() << endl; system("pause"); } VS 2010 debugger error:
1>------ Построение начато: проект: String, Конфигурация: Debug Win32 ------ 1>Построение начато 28.01.2012 18:26:23. 1>InitializeBuildStatus: 1> Обращение к "Debug\String.unsuccessfulbuild". 1>ClCompile: 1> useString.cpp 1> String.cpp 1>c:\users\vladislav\documents\visual studio 2010\projects\string\string\string.cpp(7): error C2572: String::String: переопределение параметра по умолчанию: параметр 1 1> c:\users\vladislav\documents\visual studio 2010\projects\string\string\string.h(8): см. объявление "String::String" 1> Создание кода... 1> 1>СБОЙ построения.
Tell me, please, why did it happen? I just do not understand. Thank.