Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 04-13-2010, 01:26 PM   #1
Daemon Poster
 
connchri's Avatar
 
Join Date: Mar 2005
Location: Scotland, UK
Posts: 1,019
Send a message via MSN to connchri
Default C++, I'm stuck with vectors and functions

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.
__________________

__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix"
connchri is offline   Reply With Quote
Old 04-13-2010, 06:59 PM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: C++, I'm stuck with vectors and functions

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?
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 04-14-2010, 09:39 AM   #3
Daemon Poster
 
connchri's Avatar
 
Join Date: Mar 2005
Location: Scotland, UK
Posts: 1,019
Send a message via MSN to connchri
Default Re: C++, I'm stuck with vectors and functions

Quote:
Originally Posted by berry120 View Post
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.
__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix"
connchri is offline   Reply With Quote
Old 04-14-2010, 08:35 PM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: C++, I'm stuck with vectors and functions

Can you not just do:

Code:
turbVect.at(pos).getName()
for instance?
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 04-15-2010, 02:57 PM   #5
Daemon Poster
 
connchri's Avatar
 
Join Date: Mar 2005
Location: Scotland, UK
Posts: 1,019
Send a message via MSN to connchri
Default Re: C++, I'm stuck with vectors and functions

Quote:
Originally Posted by berry120 View Post
Can you not just do:

Code:
turbVect.at(pos).getName()
for instance?
As simple as that?

I'll give it a try. And let you know how I get on.

Thanks very much! Your help is greatly appreciated!!!
__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix"
connchri is offline   Reply With Quote
Old 04-20-2010, 09:23 AM   #6
Daemon Poster
 
connchri's Avatar
 
Join Date: Mar 2005
Location: Scotland, UK
Posts: 1,019
Send a message via MSN to connchri
Default Re: C++, I'm stuck with vectors and functions

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.
__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix"
connchri is offline   Reply With Quote
Old 04-20-2010, 01:13 PM   #7
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: C++, I'm stuck with vectors and functions

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!
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 04-20-2010, 01:35 PM   #8
Daemon Poster
 
connchri's Avatar
 
Join Date: Mar 2005
Location: Scotland, UK
Posts: 1,019
Send a message via MSN to connchri
Default Re: C++, I'm stuck with vectors and functions

Quote:
Originally Posted by berry120 View Post
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
__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix"
connchri is offline   Reply With Quote
Old 04-20-2010, 01:58 PM   #9
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: C++, I'm stuck with vectors and functions

Is it because of the (1) after the declaration of turb?
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 04-20-2010, 02:08 PM   #10
Daemon Poster
 
connchri's Avatar
 
Join Date: Mar 2005
Location: Scotland, UK
Posts: 1,019
Send a message via MSN to connchri
Default Re: C++, I'm stuck with vectors and functions

Quote:
Originally Posted by berry120 View Post
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??
__________________

__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix"
connchri is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:32 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0