Hi iam begginer in C ++ I have a class with static methods and I cannot access them, this causes an error
1>------ Build started: Project: CPractice, Configuration: Debug Win32 ------ 1> Source.cpp 1>Source.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > CPractice::name" ( ?name@CPractice @@ 0V?$basic_string@DU ?$char_traits@D @ std@ @ V?$allocator@D @ 2@ @ std@ @A) 1>c:\users\innersoft\documents\visual studio 2012\Projects\CPractice\Debug\CPractice.exe : fatal error LNK1120: 1 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
and here is my code
#include <iostream> #include <stdio.h> #include <cstdlib> #include <string> using namespace std; class CPractice { public: static void setName(string s) { name = s; } static string getName() { return name; } private: static string name; }; int main() { CPractice::setName("Name"); cout << "\n" << CPractice::getName(); system("PAUSE"); return EXIT_SUCCESS; }
source share