Hi. I have a problem. This is my code.

// the array.h file 
#ifndef ARRAY_H 
#define ARRAY_H 
#include <iostream> 
#include "arr_exception.h" 
using namespace std; 
class Array { 
friend ostream &operator <<(ostream &os, const Array &arr);                           // done 
public: 
        Array(int n);                                                                                                           // done 
        Array(const Array &array);                                                                                  // done 
        ~Array() {cout << "In destructor - deleting location " << arr << 
endl; delete arr;} 
        Array &operator =(const Array &array);                                                          // done 
        int get(int index) const throw(ArrException);                                           // done 
        void set(int index, int value) throw(ArrException);                                     // done 
        int length() const {return n;}                                                                          // done 
        void print(ostream &os = cout) const;                                                               // done 
        void printPtr() {cout << "Address in arr: " << arr;}                              // done 
        int &operator[](int index);                                                                                 // done 
private: 
        int *arr; 
        int n; 
}; 

#include <iostream> 
#include <string> 
#include <fstream> 
#include "arr_exception.h" 
#include "Array.h" 
using namespace std; 
const int MAXWORDS = 200; 
class wordlist 
{ 
public: 
        string word[MAXWORDS]; 
        Array wordcount(int); 
        Array firstline(int); 
        Array lastline(int); 
        wordlist (int a) { wordcount (a); firstline(a); lastline(a);} 
        wordlist () { wordcount (200); firstline(200); lastline(200);} 
}; 

int main (int argc, char * const argv[]) { 
        cout << "Please enter a block of textsprint:  \n"; 
        string str = ""; 
        string lcstr = ""; 
        int counter = 0; 
        wordlist b; 
        for (int i=0; i <200; i++) {b.wordcount.set(i,0);  //ERROR 
                                                                b.word[i] = ""; 
                                                //ERROR         b.lastline.set(i,0); 
                                        //ERROR                 b.firstline.set(i,0); 
// at the for statement the errors occur. 
return 0; } 
// below is the actual array.cpp file 
/* 
 *  Array.cpp 
 *  cis22array 
 * 
 *  Created by Curtis S. on 3/26/08. 
 *  Copyright 2008 __MyCompanyName__. All rights reserved. 
 * 
 */ 
#include "Array.h" 
#include "arr_exception.h" 
Array::Array(int n) : arr(new int[n]), n(n) {} 
int Array::get(int index) const throw(ArrException) { 
        if (index < 0 || index >= length()) throw ArrException("In 
Array::get, index out of range"); 
        return arr[index]; 
} 

void Array::set(int index, int value) throw(ArrException) { 
        if (index < 0 || index >= length()) throw ArrException("In 
Array::set, index out of range"); 
        arr[index] = value; 
} 

Array::Array(const Array &array) : 
                                arr(new int[array.length()]), n(array.length()) { 
        for (int i = 0; i < n; i++) 
                arr[i] = array.arr[i]; 
} 

Array &Array::operator =(const Array &array) { 
        if (this == &array) return *this; 
        delete arr; 
        arr = new int[array.length()]; 
        n = array.length(); 
        for (int i = 0; i < n; i++) 
                arr[i] = array.arr[i]; 
        return *this; 
} 

void Array::print(ostream &os) const { 
        os << "["; 
        for (int i = 0; i < length(); i++) 
                os << arr[i] << (i < length()-1 ? ", " : ""); 
        os << "]\n"; 
} 

ostream &operator <<(ostream &os, const Array &array) { 
        os << "["; 
        for (int i = 0; i < array.length(); i++) 
                os << array.arr[i] << (i < array.length()-1 ? ", " : ""); 
        os << "]\n"; 
        return os; 
} 

int &Array::operator[](int index) {return arr[index];} 

// the problem is that everytime I try to compile I get the message //
in the subject line 3 times
//help please!
-- Curtis S.

Recommended Answers

All 2 Replies

so wordlist::wordcount is a function. you are trying to get a field of it?

No. wordlist is a class. wordcount is also a class. wordlist has four data members and two member functions. It has wordcount, firstline, and lastline. wordcount, firstline, and lastline are all of type Array. Array is a user defined class, not to be confused with array out of the stl. wordlist also has a fourth data member, string word[MAXWORDS]; const int MAXWORDS = 200; wordlist also includes two constructors, a default and another that allows the programmer to specify the size. The effect of both these constructors in the actual program should be the same, where all three Array classes are initialized to 200.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.