Array of Objects in a class help


Hello, i'm making a library program using classes...

My question is how can i or is there a way to initialize an array of objects with undefined size ?

Here is my class declaration:
1
2
3
4
5
6
7
8
9
10
11
12
13
class BOOKS
{
	public:
		BOOKS (string title,
			string author);
		void showTitle () { cout <<TITLE_;}
		void showAuthor() { cout <<AUTHOR_; }
	
	private:
		string TITLE_;
		string AUTHOR_;

}book[]; /*** I am having trouble in this line ***/


Please someone help me
You can use std::vector instead of the array. Or you should dynamically allocate the array and use pointer to it.
Last edited on
Oh.. i see, it seems that i have to use pointers and memory management :( to accomplish this
Okay, so i have read about dynamic allocation and try it but i can't still can't figure out how to allocate an array of an unknown size.

Here first is my class declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class BOOKS
{
	public:
		void setInfo (string title, string author)
		{
			TITLE_  = title;
			AUTHOR_ = author;
		}
		void showTitle () { cout <<TITLE_;}
		void showAuthor() { cout <<AUTHOR_; }
	
	private:
		string TITLE_;
		string AUTHOR_;
};


and here is my pointer to object declaration:
1
2
BOOKS* pBOOKS;
pBOOKS = new BOOKS [];// how can i set the array size to be undeclared 


and another question.. why can't i use the -> operator when calling the object funtion.. instead i have to use the . operator? isn't it that i have to call the function by pBOOKS[0]->setInfo( "blabla", "blabla");

pls someone help me again!
Last edited on
If you do not tell the program how much space to allocate, then it cannot allocate space.

As vlad has already suggested, for unknown sizes, use a container like std::vector. Each time you need to add a element, you can just use push_back(). Plus the container does all the memory handling for you!


For your other question, the reason you can't use -> is because you are not using a pointer. pBOOKS[n] will return the object itself, so you have already dereferenced the pointer.
Oh i see... i'm pretty new to classes and pointers and now i have to learn vectors
For now, just use a fixed size array, so you can focus on learning how to use classes and pointers. STL containers can be like the next step or something.
But when i declare a pointer like pBOOKS = new BOOKS [5]
the compiler is generating errors
Last edited on
Well, actually I meant an array on the stack, like BOOKS pBOOKS[5];.

What is the first error message you get?
the error is so long and i cant copy it on cmd window

well actually i was making a program that copy my text file content and create object for each book , this is what my file looks like:

The C++ Programming Language
Bjarne Stroustrup
The C Programming Language 2nd Edition
Daniel Kerninghan & Dennis Rithcie
Python Tutorial
Swaroop C H

i want to create an object for each book and copy the info ( title and author )

and this is my code snippet to copy the text and create an object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string title;
string author;
	
	ifstream list (CONFIG_FILE);             // Create Object
	
	int i = 0;                               // Class object Identifier ( increases by 1 every loop )
	
	BOOKS* pBOOKS;
	pBOOKS = new BOOKS [20]; // say 20
	
	if (list.is_open()) {
		while (list.good()) {
			getline (list, title);
			getline (list, author);
			pBOOKS[i].setInfo (title, author);
			i++;						   // increment index
		}
	}
Last edited on
Note, that:
1
2
3
4
5
6
7
8
9
10
while (list.good())
{
  // list is good
  getline (list, title);
  // list might be bad now
  getline (list, author);
  // Nothing guarantees that i is still <20
  pBOOKS[i].setInfo (title, author);
  i++;
}
what should i do to fix it ?
should i check after every getline if list is bad?

and why does the compiler generates very long errors.. i know that there is no problem with my syntax.. I think the problem was with the array of objects allocation
The logical and: &&. while ( A && B && C )
And is lazy; if A is false, then the rest is not evaluated at all.

why does the compiler generate very long errors

1. When one spot is "off the mark", everything after it can be off too. That generates many errors, but only the first one might be real.

2. Overloading, namespaces, and scopes. When a function call is erroneus/ambiguous, a whole list of equally bad candidates, which the compiler is aware of, is shown.

3. Template code. Standard library uses a lot of templates. You might say std::vector<int>, but the real type definition contains much more. Error messages show every detail. Combined with other points, I've heard of a 1000 line burst due to one tiny mistake.


Damned if you do, damned if you don't. Overly verbose errors make bughunt hard, but without enough data you have no idea what you are hunting.
I'm sorry, i was using earlier the command-line g++ compiler and i can't copy the error, can't even see the first lines of error because i was in cmd window

Now i've suceeded to copy the error from nppexec plug-in in notepad++

This is my function, my class declaration is posted above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void editBook()
{
	//----------  Copy File Content to memory
	//---------- (by creating object for each book)
	string title;
	string author;
	
	ifstream list (CONFIG_FILE);             // Create Object
	
	int i = 0;                               // Class object Identifier ( increases by 1 every loop )
	
	BOOKS* pBOOKS;
	pBOOKS = new BOOKS [20];
	
	if (list.is_open()) {
		while (list.good()) {
			getline (list, title);
			getline (list, author);
			pBOOKS[i].setInfo (title, author);
			i++;						   // increment index
		}
	}
	else {
		clearScreen();
		cout <<"====== Fatal Error: =======\n";
		cout <<" Unable to read file " << CONFIG_FILE << ".\n";
		cout <<" Press any key to exit.";
		getch();
		exit(1);
	}
	//---------- Start Printing contents
	cout <<"================================================\n";
	cout <<"NO." <<setw(3) <<"TITLE" <<setw(20) <<"AUTHOR\n\n";
	int j;
	for (j = 0; j < i; j++) {
		cout <<j <<setw(3) <<pBOOKS[j].showTitle() <<setw(20) <<pBOOKS[j].showAuthor() <<"\n";
	/** unfinished **/
	}
	delete [] pBOOKS;
	list.close();
}



And this one looks crazy but here is the error:

C:\Users\samsung\Documents\GCC\libraryConsole.cpp: In function 'void editBook()':
C:\Users\samsung\Documents\GCC\libraryConsole.cpp:116:44: error: no match for 'operator<<' in 'std::operator<< <char, std::char_traits<char> >((* & std::cout.std::basic_ostream<_CharT, _Traits>::operator<< <char, std::char_traits<char> >(j)), std::setw(3)) << (pBOOKS + ((sizetype)(((unsigned int)j) * 8u)))->BOOKS::showTitle()'
C:\Users\samsung\Documents\GCC\libraryConsole.cpp:116:44: note: candidates are:
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
                 from C:\Users\samsung\Documents\GCC\libraryConsole.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:106:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:106:7: note:   no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:115:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:115:7: note:   no known conversion for argument 1 from 'void' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:125:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:125:7: note:   no known conversion for argument 1 from 'void' to 'std::ios_base& (*)(std::ios_base&)'
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:164:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:164:7: note:   no known conversion for argument 1 from 'void' to 'long int'
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:168:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:168:7: note:   no known conversion for argument 1 from 'void' to 'long unsigned int'
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:172:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:172:7: note:   no known conversion for argument 1 from 'void' to 'bool'
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:607:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40,
                 from C:\Users\samsung\Documents\GCC\libraryConsole.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/ostream.tcc:93:5: note:   no known conversion for argument 1 from 'void' to 'short int'
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
                 from C:\Users\samsung\Documents\GCC\libraryConsole.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:179:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:179:7: note:   no known conversion for argument 1 from 'void' to 'short unsigned int'
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:607:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40,
                 from C:\Users\samsung\Documents\GCC\libraryConsole.cpp:1:


I can't post all the errors bacause the max letters is just 8192..
i hope the beggining lines can tell you the error

And.. I'm sorry for my laziness :))))
The most relevant compiler error is nearly always in the very first line containing "error":
C:\Users\samsung\Documents\GCC\libraryConsole.cpp:116:44: error: no match for 'operator<<' in 'std::operator<< <char, std::char_traits<char> >((* & std::cout.std::basic_ostream<_CharT, _Traits>::operator<< <char, std::char_traits<char> >(j)), std::setw(3)) << (pBOOKS + ((sizetype)(((unsigned int)j) * 8u)))->BOOKS::showTitle()'


Most important bit of info:
libraryConsole.cpp:116:44:


It's complaining about line 116, which looks like this one here:
cout <<j <<setw(3) <<pBOOKS[j].showTitle() <<setw(20) <<pBOOKS[j].showAuthor() <<"\n";

Look at the function signatures of showTitle and showAuthor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 class BOOKS
{
	public:
		void setInfo (string title, string author)
		{
			TITLE_  = title;
			AUTHOR_ = author;
		}
		void showTitle () { cout <<TITLE_;}
		void showAuthor() { cout <<AUTHOR_; }
	
	private:
		string TITLE_;
		string AUTHOR_;
};

You are calling those functions within your cout statement; therefore you're attempting to pass their return value directly to cout, but they both have void return keywords
Last edited on
oh i see.. there is cout statement inside showTitle and showAuthor yet i call the function w/ cout.... hahaa now i now.. :)))

that is just a very basic error... i feel very noob... :((
Topic archived. No new replies allowed.