• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Instantiating a class

Hello
You'll have to excuse my ignorance, but I have a class called StudentActionList, which has all these methods.  I sucessfully got everything to compile so I go to the main method, and type the following...

 #include <cstdlib>
#include <iostream>
#include "studentactionlist.h"

using namespace std;

int main()
{
    StudentActionList objStudentAction;
    objStudentAction.loadStudents();
}

And its giving me the followin gerror...Being a java developer, this is blowing my mind...How can I miss something this simple, but its giving me a hard time.  Any help would be appreciated, and since its Saturday night...I'm putting 500 points towards it.  Thank you.
0
ispcorp
Asked:
ispcorp
  • 4
  • 2
2 Solutions
 
ispcorpAuthor Commented:
Here is the code for the class I'm instantiating...

#include "studentactionlist.h"
#include<cstddef>
#include<cassert>
#include "stdlib.h"

StudentActionList::StudentActionList():StudentList()
{}
StudentActionList::~StudentActionList()
{}
StudentActionList::StudentNode *StudentActionList::getStudentByStudentNumber(char *paramStudentNumber) throw (ListIndexOutOfRangeException)
{
    bool blnIsItem;
    itSize = getLength();
    char *strStudentNumber;
    ItemStudentType objStudent;
   
    if (itSize == 0)
    {
       throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: They're no students loaded.");
    }
    else
    {
        StudentNode *objStudent = head;
        for (int i=0; i < itSize; ++i)
        {
            strStudentNumber=objStudent->item.getStudentNumber();
            if (strcmp(strStudentNumber,paramStudentNumber))
            {
               blnIsItem=true;
               break;
            }
            StudentNode *cur=cur->next;
            objStudent->item=cur->item;            
        }
        if (blnIsItem)
        {
            return objStudent;
        }
        else
        {
            return NULL;
        }
    }
}
void StudentActionList::deleteStudent(ItemStudentType *paramStudent) const throw(ListIndexOutOfRangeException)
{
     itSize = getLength();
     //ItemStudentType objStudent;
     if (itSize == 0)
     {
        throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: Item list is empty.");
     }    
     else
     {
         StudentNode *objStudent = head;
         StudentNode *prev=NULL;
         for (int i=0; i < itSize; ++i)
         {
             StudentNode *tmp=objStudent;
             prev=objStudent;
             objStudent=objStudent->next;
             if (strcmp(tmp->item.getStudentNumber(),paramStudent->getStudentNumber()))
             {
                if (prev!=NULL) prev->next = objStudent;
                delete tmp;
             }
             //StudentNode *cur=cur->next;
             //objStudent->item=cur->item;          
         }
     }
}
void StudentActionList::loadStudents()
{
     StudentNode *objStudent = new StudentNode();
     ItemType temp;
     ifstream in("test.csv");
     vector < vector <string> > data;
     string element, delimiters = ",\n\r";
     int row = 0;
     char ch;

     data.push_back( vector <string>() );

     while( in.read( (char*)&ch, 1 ) )
     {

          if( delimiters.find_first_of(ch) == delimiters.npos )
          {
               element += ch;
          }
          else
          {
               if( ch != '\r' )
               {
                    data[row].push_back( element );
                    element = "";

                    if( ch == '\n' )
                    {
                         data.push_back( vector <string>() );
                         row++;
                    }
               }
          }
     }

     if( element.size() > 0 )
          data[row].push_back( element );

     in.close();

     for( unsigned int x = 0; x < data.size(); x++ )
     {
          ItemType *objStudenType = new ItemType();
          objStudent->item.setStudentNumber((char*)data[x][0].c_str());
          objStudent->item.setFirstName((char*)data[x][1].c_str());
          objStudent->item.setLastName((char*)data[x][2].c_str());
          objStudent->item.setStudentYear((char*)data[x][3].c_str());
          objStudent->item.setCredits(atoi((char*)data[x][4].c_str()));
          objStudent->item.setGPA(atoi((char*)data[x][5].c_str()));
          insert(getLength(),objStudent->item);
          //for( unsigned int y = 0; y < data[x].size(); y++ )
          //{
          //    cout << data[x][y] << ",";
          //}
          //cout << endl;
     }
}


0
 
ispcorpAuthor Commented:
Whoops, here is the error...

studentactionlist.cpp C:\Project1\studentactionlist.o(.bss+0x0) multiple definition of `itSize'
main.cpp C:\Project1\main.o(.bss+0x0) first defined here
0
 
efnCommented:
I suspect the problem lies in the studentactionlist.h file.  Can you post it?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Jaime OlivaresSoftware ArchitectCommented:
maybe your studentactionlist.h file need "header guards". Something like this:


studentactionlist.h
---------------------

#ifndef STUDENTACTIONLIST_H
#define STUDENTACTIONLIS_H

// all your studentactionlist.h contents here

#endif
0
 
ispcorpAuthor Commented:
I found it, you're right, it was a problem with stuaction.h file.  I was declaring int itSize before the class declaration, and it was giving me an error.  I have no idea why it would do that.   Tell me, in what instances would you post declarations above the class?  I know I do it for typedef and enums.  Is there a rule?  Is that where you store your static variables?  
0
 
Jaime OlivaresSoftware ArchitectCommented:
I think the problem is not declaration position. The problem is that every time you call stuaction.h file, a new declaration is done, so that's why there is a duplicated declaration.
Header guard ensure that declaration is made just one, even if you call the header file twice.
By example, if you accidentally call stuaction.h twice:

#include "stuaction.h"
#include "someother.h"
#include "stuaction.h"

will not generate an error with header guards.
That could appear to be uncommon, but occurs, by example, when "someother.h" also have an include to the same header.
0
 
ispcorpAuthor Commented:
Your both right, I was receiving two errors, and both of you came up with two solutions.  I gave jaime the larger amount of points because he was more specific, and answered my problem in a specific manner.  
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now