|
|
#1 |
|
Daemon Poster
|
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>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" |
|
|
|
|
|
#2 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
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. |
|
|
|
|
|
#3 | |
|
Daemon Poster
|
Quote:
I'm unsure how to do this. Cheers. CW.
__________________
Delta: "What's wrong Chris?? Chris: "I miss my old Cyrix" |
|
|
|
|
|
|
#4 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
Can you not just do:
Code:
turbVect.at(pos).getName()
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
|
#5 | |
|
Daemon Poster
|
Quote:
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" |
|
|
|
|
|
|
#6 |
|
Daemon Poster
|
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" |
|
|
|
|
|
#7 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
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. |
|
|
|
|
|
#8 | |
|
Daemon Poster
|
Quote:
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_hppThis 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" |
|
|
|
|
|
|
#9 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
Is it because of the (1) after the declaration of turb?
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
|
#10 |
|
Daemon Poster
|
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) 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" |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|