Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

C++ vector problem

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes, I finally began learning the STL! It took some time but I'm there. I'm familiar with C# ArrayList, etc., so I'm glad I can relate pretty well. I came across an odd error. If someone can spot it, I can only learn from it :) This program asks the user for a name and it tries to find it and gives out the person's age. It's basically a simple in-game database of player objects. Thanks in advanced for the help.
#include <iostream>  
#include <string>
#include <vector>
using namespace std;  
   
class Common
{
public:   
	string name;
	int age;

	Common(string name, int age) 
	{
		this->name = name;
		this->age = age;
	};
};

int main()
{    
	Common PC1("Philly", 23);
	Common PC2("Jilly Dilly", 400);

	vector<Common> v(10);
	v.push_back(PC1); 
	v.push_back(PC2);

	cout << "Enter a name to find: ";
	string sBuffer;
	cin >> sBuffer;

	bool found = false;

	for (unsigned short int i = 0; i < v.size(); i++)
	{
		if (v.name == sBuffer)
		{
			cout << "Found it! The age is " << v.age << "." << endl;
			found = true;
		}
	}	

	if (!found)
		cout << "The requested name could not be found." << endl;

	system("pause");
	return 0;
}

Error Message:
------ Build started: Project: Console Practice, Configuration: Debug Win32 ------
Compiling...
Console Practice.cpp
C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(470) : error C2512: 'Common::Common' : no appropriate default constructor available
        C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(467) : while compiling class template member function 'std::vector<_Ty>::vector(__w64 unsigned int)'
        with
        [
            _Ty=Common
        ]
        .\Console Practice.cpp(23) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
        with
        [
            _Ty=Common
        ]
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Console Practice\Console Practice\Debug\BuildLog.htm"
Console Practice - 1 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
Advertisement
You're trying to create a vector filled with 10 Commons that are default constructed. Your Common class has no default construtor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!