Unresolved externals when Vector1 = Vector 2

Hello!

I have been burning my brain for the last three hours trying to understand why my code wouldn't compile because of linking problems. These are the errors:

Error	10	error LNK1120: 7 unresolved externals	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Debug\Helper.exe	Helper
Error	7	error LNK2001: unresolved external symbol "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\Clickable.obj	Helper
Error	8	error LNK2001: unresolved external symbol "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\MenuItem.obj	Helper
Error	9	error LNK2019: unresolved external symbol "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ) referenced in function "public: __thiscall std::_Vector_alloc<0,struct std::_Vec_base_types<class MenuItem,class std::allocator<class MenuItem> > >::~_Vector_alloc<0,struct std::_Vec_base_types<class MenuItem,class std::allocator<class MenuItem> > >(void)" (??1?$_Vector_alloc@$0A@U?$_Vec_base_types@VMenuItem@@V?$allocator@VMenuItem@@@std@@@std@@@std@@$$FQAE@XZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\Options.obj	Helper
Error	2	error LNK2020: unresolved token (0A000140) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\MenuItem.obj	Helper
Error	3	error LNK2020: unresolved token (0A000142) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\Clickable.obj	Helper
Error	5	error LNK2020: unresolved token (0A000187) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\MenuItem.obj	Helper
Error	6	error LNK2020: unresolved token (0A000188) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\Clickable.obj	Helper
Error	1	error LNK2028: unresolved token (0A000141) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ) referenced in function "public: __thiscall std::_Vector_alloc<0,struct std::_Vec_base_types<class MenuItem,class std::allocator<class MenuItem> > >::~_Vector_alloc<0,struct std::_Vec_base_types<class MenuItem,class std::allocator<class MenuItem> > >(void)" (??1?$_Vector_alloc@$0A@U?$_Vec_base_types@VMenuItem@@V?$allocator@VMenuItem@@@std@@@std@@@std@@$$FQAE@XZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\Options.obj	Helper
Error	4	error LNK2028: unresolved token (0A000188) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ) referenced in function "public: __thiscall std::_Vector_alloc<0,struct std::_Vec_base_types<class MenuItem,class std::allocator<class MenuItem> > >::~_Vector_alloc<0,struct std::_Vec_base_types<class MenuItem,class std::allocator<class MenuItem> > >(void)" (??1?$_Vector_alloc@$0A@U?$_Vec_base_types@VMenuItem@@V?$allocator@VMenuItem@@@std@@@std@@@std@@$$FQAE@XZ)	C:\Users\Ze\Documents\Visual Studio 2012\Projects\Helper\Helper\Options.obj	Helper


I learned through google that usually a problem like that is caused by calling a function without a body. I couldn't find any function that had no body, so I commented out all files one by one to find where the problem was, and I found that it was a specific line in a Class called MenuItem, which inherits from a class called Clickable.

The line that caused the problem is the first one of my constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MenuItem(SDL_Rect Rect, SDL_Surface* Surface, vector<MenuItem> SubItems)  : Clickable(Rect, Surface)
	{
                //first I tried like this
                m_SubItems = SubItems;

                //then this
		vector<MenuItem>::iterator iter_beg = m_SubItems.begin();
		vector<MenuItem>::iterator iter_end = m_SubItems.end();
		m_SubItems.assign(iter_beg, iter_end);

                
		//also tried using an initializer list
	}



What could be the problem? When I comment everything out, it builds fine.

Also, at first I had the class constructor declaration separate from the definition, but I kept getting an error from the compiler complaining that there should be a curly bracket after this:

MenuItem(SDL_Rect Rect, SDL_Surface* Surface, vector<MenuItem> SubItems) : Clickable(Rect, Surface);

so I had to move the definition to the same place as the declaration in order to get rid of it. Is my syntax wrong?

Thanks a lot!
Last edited on
When you define a custom constructor, the compiler stops giving you the "default" default and copy constructors...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Okay
class test
{
  public:
  int x;
};

int main(void)
{
  test first;
  first.x = 1234;

  test second(first);
  cout << second.x << std::endl;
  return 0;
}


// Not Okay
class test
{
  public:
  int x;

  test(int y) : x(y) {} // note that you do need braces
};

int main(void)
{
  test first;  // Error: no default constructor found

  test first(1234);

  test second(first);  // still works
  cout << second.x << std::endl;
  return 0;
}


You should be passing that vector by reference too.
Last edited on
Thank you very much for your answer! I actually managed to solve it yesterday with a friend's help. The problem is that Visual Studio was thinking I was trying to build in debug mode, when I was actually trying to build in release mode (or something like that). What fixed it was going to Project > Properties > C/C++ and removing _DEBUG (or any variant of this, I don't remember exactly) from Preprocessor Definitions.

Also, about the issue of needing a bracket after the declaration, I learned that I can omit the initializer list from the declaration and have it on the definition instead, thus enabling me to separate definition and declaration.
Anything that can be done should be done in an initializer list.

The initializer list goes in the definition:
1
2
3
MyClass(int x);

MyClass(int x) : val(x) {}
Topic archived. No new replies allowed.