GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-May-2006, 23:18
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Assigning an object to an element of an indexed list


I'm lost again in the whole interaction between main, a "candidate" class, and an "Indexed LIst" template class. In the indexed list, I'm trying to assign the values from an object of type candidate to an element of an array. My compiler doesn't like this and tells me:
Quote:
C11PP3 has exited due to signal 10 (SIGBUS).
I can't see that I'm doing anything illegal. Help!!
main:
CPP / C++ / C Code:
#include "candidate.h"
#include "indexList.h"

#include <iostream>
using namespace std;

int main () {
       
    indexList<candidate> slate;
    candidate newCand;
    slate.append(newCand);
        
    return 0;
}
indexList.h:
CPP / C++ / C Code:
#ifndef INDEXLIST_H
#define INDEXLIST_H

#include "candidate.h"
#include <iostream>
using namespace std;

template<class T>
class indexList
{
    public:
        indexList()
        {
            size = 0;
        }
        bool append(const candidate& newCand)
        {
            size++;
            elements[size - 1] = newCand;
        }

    private:
        T elements[];
        int size;
};
#endif
candidate.h:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

#ifndef CANDIDATE_H
#define CANDIDATE_H

class candidate
{
    public:
        //Constructor
        candidate();
                
    private:
        string name;
        int votes[5];
        int total;
 };
#endif
candidate.cpp:
CPP / C++ / C Code:

#include "candidate.h"
#include <iostream>
using namespace std;

//Constructor
candidate::candidate()
{
    name = "";
    for (int i = 0; i < 5; i++)
        votes[i] = 0;
    total = 0;
}
        
  #2  
Old 25-May-2006, 03:56
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: assigning an object to an element of an indexed list


Hi - your problem is that there is nothing in your code to set/change the size of elements[] (to specify the number of pointers that are held within elements[]). If you want to 'grow' the array then you have to tell it so.

CPP / C++ / C Code:
bool append(const candidate& newCand)
        {
            size++;
            elements[size - 1] = newCand;
        }

    private:
        T elements[]; //NO SIZE SPECIFIED
        int size; //THIS DOES NOT HAVE ANY REAL CONNECTION TO elements[] 
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Help in C Print is not working with LinkList batman3280 C Programming Language 3 09-Mar-2006 20:03
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 15:13
search linked list itsmecathys C++ Forum 20 18-Apr-2005 02:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.