C++, I'm stuck with vectors and functions

connchri

Daemon Poster
Messages
1,025
Location
Scotland, UK
Hello all, long time no, erm, type?

Anyway,

I'm having to do a c++ project for university and it's to simulate the power output of a windfarm.

Long story short, I have created a header file that contains my object template and class functions for my windturbines.

I then want to use this windturbine template/object (sorry, I'm not too hot on the terminology) to create a vector. This vector will be used to represent the windfarm made of a number of turbines.

ie. here's the header file for the turbine:

#include <iostream>
#include <string>
#include <math.h>
#define PI 3.14159265
#define AirDensity 1.225
using namespace std;

class turbine
{
private:
string name;
int BladeLength;
int GeneratingCapacity;
float minSpeed;
float maxSpeed;
public:
turbine();
~turbine();
string getName(){return name;}
void setName (string n){name = n;}
int getBladeLength(){return BladeLength;}
void setBladeLength(int a){BladeLength = a;}
int getGeneratingCapacity(){return GeneratingCapacity;}
void setGeneratingCapacity(int b){GeneratingCapacity = b;}
float getminSpeed (){return minSpeed;}
void setminSpeed (float a){minSpeed = a;}
void setmaxSpeed (float a){maxSpeed = a;}
int calculatePower (float Windspeed);
};​

The function for calculatePower is held in turbine.cpp, but as it's irrelavent for the question I've not included it here.

Here is the line of code form my windfarm class for creating a vector of turbine objects to represent the windfarm (this is held in a test.cpp, as I use that for testing code as I go along):

vector <turbine> turb(1);​

My question is, is there anyway to access the functions of the turbine class (primerally the setters, getters and calculatepower functions) for a vector element in the vector turb?

I realise I could rewrite my code so that the functions are part of the windfarm class, but I would like to reuse the windturbine code elsewhere in the program and hence avoid this.

Any help would be greatly appreciated.

Cheers,

CW.
 
Forgive me if I'm missing something obvious here (years since I've looked at C/C++ and it shows!) but can you just not use the at() method on the vector to grab a reference to a turbine at any particular position?
 
Forgive me if I'm missing something obvious here (years since I've looked at C/C++ and it shows!) but can you just not use the at() method on the vector to grab a reference to a turbine at any particular position?

Yeah, I can access the data held in the vector with the at() method, but how do I access the functions(methods) of the datatype held at that point in the vector?

I'm unsure how to do this.

Cheers.

CW.
 
Just tried that, and it worked.

Thanks very much! It was actually simpler than I thought. Well, it is when you know anyway.

Cheers,

CW.

Now I have another question.

After successfully getting the functions to work, I now need to know how to add another element to my vector.

My understanding is that I need to use push_back(<value>), but how is this possible when the vector contains objects of type turbine? (See code in my 1st post). What sort of <value> am I ment to pass?

Cheers,

CW.
 
You'd pass a reference or a pointer to an object of type turbine.

In the nicest possible way, it does sound like you're trying to run before you can walk a bit with C++ - I'd honestly drop the vector bit for now and do some reading up on pointers, references and the like. A bit of background reading on them and basic C++ syntax won't take ages and should solve a lot of your problems. At the very least you'll understand a lot more of how your code works and why it's done that way!
 
You'd pass a reference or a pointer to an object of type turbine.

In the nicest possible way, it does sound like you're trying to run before you can walk a bit with C++ - I'd honestly drop the vector bit for now and do some reading up on pointers, references and the like. A bit of background reading on them and basic C++ syntax won't take ages and should solve a lot of your problems. At the very least you'll understand a lot more of how your code works and why it's done that way!

I completely hear you. The thing is though, it's a module for my engineering degree, and so it isn't something we've had a lot of time to do. I think the learning methology the lecture is usings is a case of heres a program you've to code, now here's the notes, learn and get on with it.

I understand references and pointers - I just think I'm awful inexperienced at the moment. I've figured how to add an element. I'm just getting compiler errors now with my wind farm structure.

Anyway, one last final question, and I'll stop pestering.

Here's code for my final windfarm class (excluding the functions held in a cpp file):

#ifndef windfarm_hpp
#define windfarm_hpp

#include <iostream>
#include <vector>
#include <string>
#include "turbine.h"

#endif

using namespace std;

class windfarm
{
private:
string Farmname;
string windfilepath;
vector <turbine> turb(1);
int FarmPowerCapacity;
int FarmPowerGeneration;
int FarmGeneratingCapacity;

public:
windfarm();
~windfarm();

//Functions for maniplulating individual turbines (SETTERS)
void setTurbName(int pos, string name){turb.at(pos).setName(name);}
void setTurbBladeLength(int pos, int length){turb.at(pos).setBladeLength(length);}
void setTurbGeneratingCapacity(int pos, int power){turb.at(pos).setGeneratingCapacity(power)};
void setTurbMinSpeed(int pos, float speed){turb.at(pos).setminSpeed(speed);}
void setTurbMaxSpeed(int pos, float speed){turb.at(pos).setmaxSpeed(speed);}

//Functions for maniplulating individual turbines (GETTERS)
string getTurbName(int pos){return turb.at(pos).getName();}
int getTurbBladeLength(int pos){return turb.at(pos).getBladeLength();}
int getTurbGeneratingCapacity(int pos){return turb.at(pos).getGeneratingCapacity()};
float getTurbMinSpeed(int pos){return turb.at(pos).getminSpeed();}
float getTurbMaxSpeed(int pos){return turb.at(pos).getmaxSpeed();}

//Functions for individual turbines (FUNCTIONS)
int calculateTurbPower(int pos, float windspeed){return turb.at(pos).calculatePower(windspeed);}

//Funtions for Windfarm
int getWindFarmSize(){return turb.size();}
void setWindFarmName(string nam){Farmname = nam;}
string getWindFarmName(){return Farmname;}
void setWindFilePath(string path){windfilepath=path;}
string getWindFilePath(){return windfilepath;}

//Function Prototypes for Windfarm
void addTurbine(string turbname, int Bladelength, int GeneratingCapacity, int minSpeed, int maxSpeed);
void removeTurbine(int turbno);
int calcWindFarmPower(float Windspeed);
int getGeneratingCapacity();
};

This is so I can access all the setters and getters, etc, from an instance of windfarm.

However, the compiler keeps throwing out an error saying that the vector, called turb, is constant, and hence a load of other errors saying that the .at function of the vector must contain a struct/class/union. I'm guessing this is because it is not correctly creating a vector of element type turbine.

Can you point out where I have gone wrong in creating this vector? The turbine class is the same as in my 1st post.

Should I have created a structure inside the class?

Thanks
 
Is it because of the (1) after the declaration of turb?

Well, that got rid of the constant error, but I'm still getting the following type of errors for each of the getter and setter methods in the windfarm.hpp.

error C2228: left of '.at' must have class/struct/union

It's as though it's not recognizing the vector called turb.

Hmmmmmm. I need my partner. Thing is, he's stuck in Pakistan untill this volcano carry on is sorted out :(

Ahhhh, I have replaced all my
Code:
turb.at(pos)
with
Code:
turb().at(pos)
.

I'm assuming this is because it is a object defined in a class instead of a single variable object like an int??
 
Back
Top Bottom