Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code below does not link as indicated Kindly advise Thank You

C++
struct cfoobar
{
	struct cgoobar
	{
		void goobar() const {}
	};
	static cgoobar m_goobar; // error LNK2001: unresolved external symbol "public: static struct cfoobar::cgoobar cfoobar::m_goobar" (?m_goobar@cfoobar@@2Ucgoobar@1@A)
};

int main()
{
	cfoobar::m_goobar.goobar();
}


What I have tried:

. learned if m_goobar not static no link error of course a cfoobar is first constructed in this case
. m_goobar re-declared outside of class still link error
Posted
Updated 10-Dec-21 17:10pm

As for basic type, you have to initialize the static variable before you can use it
C++
struct cfoobar
{
    struct cgoobar
    {
        void goobar() const {}
    }
    static cgoobar m_goobar;
};

cfoobar::cgoobar::m_goobar;  //initializes m_goobar

int main()
{
    cfoobar::m_goobar.goobar();
}
 
Share this answer
 
C++
#define FUNCSIG cout << __FUNCSIG__ << endl;

struct cfoobar
{
	struct cgoobar
	{
		void goobar() const { FUNCSIG }
	};
	static cgoobar m_goobar;
};

// error C2039 : 'm_goobar' : is not a member of 'cfoobar::cgoobar'
// cfoobar::cgoobar::m_goobar;  //initializes m_goobar

//error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
//cfoobar::m_goobar;

// this works
cfoobar::cgoobar cfoobar::m_goobar;

int main()
{
	cfoobar::m_goobar.goobar();
}
 
Share this answer
 
Comments
k5054 10-Dec-21 23:26pm    
Its late and I goofed on typing the text. Ten lashes with a wet noodle for me, but it was enough to get you over the hump, I hope.
BernardIE5317 10-Dec-21 23:30pm    
Yes Thanks I would have bet my life I tried the external re-declare as stated in my post Several times in fact All I did was reboot Visual Studio and follow your lead and Bingo Presto it works now - Thanks again - Cheerio

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900