?
Solved

Instantiating a class

Posted on 2005-03-26
7
Medium Priority
?
205 Views
Last Modified: 2010-08-05
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
Comment
Question by:ispcorp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 2

Author Comment

by:ispcorp
ID: 13638152
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
 
LVL 2

Author Comment

by:ispcorp
ID: 13638155
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
 
LVL 15

Assisted Solution

by:efn
efn earned 600 total points
ID: 13638434
I suspect the problem lies in the studentactionlist.h file.  Can you post it?
0
Technology Partners: 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!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13639112
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
 
LVL 2

Author Comment

by:ispcorp
ID: 13639114
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1400 total points
ID: 13639132
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
 
LVL 2

Author Comment

by:ispcorp
ID: 13639239
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question