Jump to content
  • Advertisement
Sign in to follow this  
SDZD

Struct-based vectors

This topic is 4818 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

Hi. I've heard that this is one of the best forums for progamming, so let's give it a go... I've been having some trouble with a probability checker that I'm writing. I'm trying to create a vector that holds a structure (hence the title), but I don't know how to access any of the structures variables. I've tried
vector(0).structure.variable1 = x
but that doesn't seem to work. How am I supposed to do it? Also, how do I initialize it (say, with pushback()), and include the variables at the same time? Thanks! ~SDZD

Share this post


Link to post
Share on other sites
Advertisement
Is it a std::vector<>? If so, you can use standard indexing syntax:
struct Thing {int variable;};
std::vector<Thing> vec(5); // Makes room for (at least) 5 entries
for (int i = 0; i < 5; ++i) {
vector.variable = i;
}
You can also use iterators if you prefer. Anyway, can't guarantee I didn't make a mistake in the example, but perhaps that will answer your question.

[Edit: nm]

Share this post


Link to post
Share on other sites
sorry, didnt see the ending question....to initialize call push_back like this:


// Adds a new structure to the array
vectorName.push_back( MyStructure( parameter1, ..., parameterN ) );

the MyStructure( parameter1, ..., parameterN ) creates an anonomyous structure and uses the constructor that accepts parameter1 to parameterN arguments.

Share this post


Link to post
Share on other sites
and for constructung them


struct Foo{
int bar;
Foo( int integer )
{
bar = integer;
}
};


int main()
{
vector<Foo> vec;

vec.push_back( Foo(9) );
vec.push_back( Foo(4) );
}




[EDIT] late ... as usual

hope this helps

Share this post


Link to post
Share on other sites
Quote:
Original post by Firewalker
sorry, didnt see the ending question....to initialize call push_back like this:


// Adds a new structure to the array
vectorName.push_back( MyStructure( parameter1, ..., parameterN ) );

the MyStructure( parameter1, ..., parameterN ) creates an anonomyous structure and uses the constructor that accepts parameter1 to parameterN arguments.


Actually, that's what I've been trying. The code is here:

backround.h

#ifndef BACKROUND_H
#define BACKROUND_H
#include "vector.h"

class Storage
{
public:
Storage();
void Vec_read();
void Vec_write(int);
~Storage();
private:
struct numfreq
{
int num;
int freq;
};
vector<numfreq> numstore;
};

#endif



backround.cpp

#include "backround.h"

Storage::Storage()
{
numstore.push_back(numfreq(1, 0));
//vectorName.push_back( MyStructure( parameter1, ..., parameterN ) );
//numstore[0].num = 1;
//numstore[0].freq = 0;
}

Storage::~Storage()
{
}

void Storage::Vec_read()
{
}

void Storage::Vec_write(int number)
{
vector<numfreq>::iterator search = numstore.begin();

}



Whenever I try to compile it, I get
 5 C:\Dev-Cpp\backround.cpp numfreq(int, int)' 
error C:\Dev-Cpp\backround.h:14 candidates are: Storage::numfreq::numfreq()
14 C:\Dev-Cpp\backround.h Storage::numfreq::numfreq(const Storage::numfreq&)


I'm using Dev-C++, in case that helps.

Share this post


Link to post
Share on other sites
the problem is you have no constructor to accept the arguments, only a default constructor and a copy constructor created by the compiler. change your numFreq structure to this:



struct numfreq
{
numFreq( int i, int j ) : num(i), freq(j)
{}

int num;
int freq;
};


the num(i), freq(j) is an initializer list. it will set the num and freq members to the values i and j respectively, when this overloaded version of the constructor is called.

Share this post


Link to post
Share on other sites
Your numfreq structure needs a constructor. It'll need a copy constructor if there are any weak references in it (there aren't in your current structure, but I'm including this for completeness).

Share this post


Link to post
Share on other sites
you havent defined a constucter that takes two ints as arguments

the compile provides two default ones, which should be overridden in almost any circumstance

the default

class Class;

Class::Class()
is defined which calls te default constuctors of every member, and leaves primitives with random garbage.

Class::Class( const Class& )
this is the default copy constuctor, which is invoked whenever you say

Class c1;
Class c2 = c1;

the compiler was complaining because neither of these take two ints...

[EDIT] dammit firewalker, i concede to your speed and will retire in shame

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!