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
Struct-based vectors
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
Is it a std::vector<>? If so, you can use standard indexing syntax:
[Edit: nm]
struct Thing {int variable;};std::vector<Thing> vec(5); // Makes room for (at least) 5 entriesfor (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]
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.
// 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.
and for constructung them
[EDIT] late ... as usual
hope this helps
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
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.
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.
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.
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).
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement