I'm still really green at this, but I've been working at this for 2 hours now and I'm stuck. Can someone help me? I'll post the code first, then the error underneath.

thanks everyone!

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
 
class Resource {
public:
 string Name_;
 int Food_;
 int Production_;
 int Commerce_;
 string ReqImprovement_;
public:
 Resource( string myName, int myFood, int myProduction, int myCommerce, string myImprovement ) 
  : Name_(myName), Food_(myFood), Production_( myProduction), 
   Commerce_( myCommerce), ReqImprovement_(myImprovement){};
 Resource(  const Resource & myResource );
 const void setResource(string myName, int myFood, int myProduction, int myCommerce, string myImprovement);
};
const void Resource::setResource(string myName, int myFood, int myProduction, int myCommerce, string myImprovement){
 Name_ = myName;
 Food_ = myFood;
 Production_ = myProduction;
 Commerce_ = myCommerce;
 ReqImprovement_ = myImprovement;
}
 
int main() {
vector<Resource> vResource; //("Name", food, prod, comm, "Required")
//these two lines work.
Resource tempResource(  "Aluminum", 0,1,0, "Mine");  
vResource.push_back(tempResource);
 
//this line doesn't
vResource.push_back( Resource( new Resource("Wheat", 1,0,0,  "Farm") ) );
 
return 0;
}

the error
======
linux:~/Desktop/project3 # g++ civ_v2.cpp
civ_v2.cpp: In function `int main()':
civ_v2.cpp:193: error: no matching function for call to `Resource::Resource(
Resource*)'
civ_v2.cpp:91: error: candidates are: Resource::Resource(const Resource&)
civ_v2.cpp:89: error:
Resource::Resource(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, int, int, int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)

Member Avatar for iamthwee

My guess is you wouldn't be able to use a vector like that.

Can't you use a regular array to store your objects?

Though I dont know a lot of C++ but here goes nothing...

The reason it is not working is because you have declared your vResource as vector vResource<Resource> ; i.e. a vector of resource objects while you are trying to push a pointer to the resource type ( new returns a pointer which points to the memory which has been allocated by requst ).

Try something like:

int main() 
{
    vector<Resource> vResource; 
    //this line doesn't
    Resource r1( "wheat", 1, 0, 0, "farm" ) ;
    vResource.push_back( r1 ) ; 
    return 0;
}

Hope it helped ,bye.

the new method return a pointer to the object you allocate in the heap,but the vResource you declare is vector<Resource>, so the vResource is designed for containing object, not pointer, you can declare the vResource lick this vector<Resource*> vResource

Member Avatar for iamthwee

Though I dont know a lot of C++ but here goes nothing...

The reason it is not working is because you have declared your vResource as vector vResource<Resource> ; i.e. a vector of resource objects while you are trying to push a pointer to the resource type ( new returns a pointer which points to the memory which has been allocated by requst ).

Try something like:

int main() 
{
    vector<Resource> vResource; 
    //this line doesn't
    Resource r1( "wheat", 1, 0, 0, "farm" ) ;
    vResource.push_back( r1 ) ; 
    return 0;
}

Hope it helped ,bye.

He/she tried that here...

vector<Resource> vResource; //("Name", food, prod, comm, "Required")
//these two lines work.
Resource tempResource(  "Aluminum", 0,1,0, "Mine");  
vResource.push_back(tempResource);
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
 
class Resource {
public:
 string Name_;
 int Food_;
 int Production_;
 int Commerce_;
 string ReqImprovement_;
public:
 Resource( string myName, int myFood, int myProduction, int myCommerce, string myImprovement ) 
  : Name_(myName), Food_(myFood), Production_( myProduction), 
   Commerce_( myCommerce), ReqImprovement_(myImprovement){};
 Resource(  const Resource & myResource );
 const void setResource(string myName, int myFood, int myProduction, int myCommerce, string myImprovement);
};
const void Resource::setResource(string myName, int myFood, int myProduction, int myCommerce, string myImprovement){
 Name_ = myName;
 Food_ = myFood;
 Production_ = myProduction;
 Commerce_ = myCommerce;
 ReqImprovement_ = myImprovement;
}
 
int main() {
vector<Resource*> vResource; //("Name", food, prod, comm, "Required")
//these two lines work.
Resource tempResource(  "Aluminum", 0,1,0, "Mine");  
vResource.push_back(&tempResource);
 
//this line doesn't
vResource.push_back( new Resource("Wheat", 1,0,0,  "Farm")  );
 
return 0;
}
Member Avatar for iamthwee

Yep that looks gud. Also look at the boost library and the so called smart pointer.

He/she tried that here...

Yeah I know that..but I also pointed out in my post why the code is not working, that was a hint to put the OP on the right track.

The reason it is not working is because you have declared your vResource as
vector vResource<Resource> ;
i.e. a vector of resource objects while you are trying to push a pointer to the resource type ( new returns a pointer which points to the memory which has been allocated by requst ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.