Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Accessing vector elements for class objects
New Posts  All Forums:Forum Nav:

Accessing vector elements for class objects

post #1 of 5
Thread Starter 
How do you access an int value in a data structure using vectors?

For instance, let's say I have a class Node.

Code:
class Node
{
public:
            Node(int v) { value = v; }
            int getValue() { return value; }
private:
            int value;
}
After creating a vector of Nodes, I want to access the 5th element's value via the getValue() Node method.
post #2 of 5
vector[4].getValue();
I would assume that would work. To use a function of a class you use class.function(). In this case vector[4] is pointing to your 5th node so you would use the above statement.
Edited by superhead91 - 4/21/11 at 12:43pm
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #3 of 5
Thread Starter 
Hi superhead91,

Thanks a bunch for your reply. That's how I was accessing it but I kept getting errors, which I now know was because I was trying to change the values by altering the returned value (can't be done...duh).

Here's a snippet of code I'm working with that I think should be enough to get by. The node class is similar to what you saw above.

Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "sort.h"
using namespace std;

void sort::selectionSort(vector<node> v)
{
    time_t start, end;
    start = time(NULL);
    for(int i = 0; i < (v.size()-1); i++)
    {
        for(int j = 1; j < v.size(); j++)
        {
            if(v[j].getData() < v[i].getData())
            {
                swap(v, j, i);
            }
        }
    }
    end = time(NULL);
    cout << end-start << " seconds elapsed while sorting " << v.size() << " items with Selection sort.\
";

    for(int i = 0; i < v.size(); i++)
        std::cout << v[i].getData() << endl;
}

void swap(vector<node> a, int small, int big)
{
    int d = a[small].getData();
    //a[small].push_back(node(a[big].getData()));
    //a[big].push_back(node(a[small].getData()));
    //a(3);
    //a[big](10);
    a[small].setData(a[big].getData());
}
The error message I keep getting is

undefined reference to `sort::swap(std::vector<node, std::allocator<node> >, int, int)'


EDIT: I figured out the problem. I wasn't properly defining the method in the implementation file.

Instead of void sort::swap, I was doing void swap. Thanks very much for your help. +1
Edited by floatingDivs - 4/21/11 at 1:15pm
post #4 of 5
Ahh... Yeah, when you define the functions you have to include the class:: Good luck!
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #5 of 5
You need to either put swap into the same class, or move the swap function above where you're calling it. You're calling it before you've defined it.

Also, to perform work on that vector when passing it into a function you need to pass it by reference (e.g. vector<node>& a, rather than vector<node>), if you don't pass by reference you'll be performing 'swap' on a copy of the vector, a copy which goes out of scope at the end of your function call.

EDIT: Sorry didn't see your edit, looks like you figured out the swap stuff. Also, why setData and getData instead of using the equal operator? e.g. a[small] = a[big]. Also it doesn't seem like it's actually swapping, it's just setting small to whatever big is.
Edited by lordikon - 4/21/11 at 5:25pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Accessing vector elements for class objects