I've been banging my head on this errors and can't seem to find where I went wrong. For any of the "set_" errors, set_ (as well as print) refers to a member function of another class . Please, any help would be great.

test.cpp: In member function `void CDCollection::get(int, std::ofstream&)':
error: `print' is not a type
error: request for member of non-aggregate type before '(' token

test.cpp: In member function `void CDCollection::add(std::istream&)':
error: `set_artist' has not been declared

error: request for member of non-aggregate type before '(' token
error: `set_title' has not been declared
error: request for member of non-aggregate type before '(' token

error: `set_year' has not been declared
error: request for member of non-aggregate type before '(' token
error: `set_descrip' has not been declared
error: request for member of non-aggregate type before '(' token

Execution terminated

class:

class CDCollection
{
	public:
        CDCollection();
        void print(ofstream &output);
		void get(int num_cds, ofstream &out);
		void add(istream& in);
	
	
	
	private:
		int num_cds, cdTotal, max;
		char cdArray[100];
};

CDCollection::CDCollection()
{
	cdTotal = 0;
}

void CDCollection::get(int num_cds, ofstream &out)
{
	cdArray[num_cds].print(out);
}
void CDCollection::print(ofstream &out)
{
	out << "CD COLLECTION:" << endl;
	for (int num_cds = 0; num_cds < cdTotal; num_cds++)
	{	
		get(num_cds, out);
	}
}
void CDCollection::add(istream& in)
{
	max = 100;
	if (cdTotal < max)
	{
		SString artist;
		SString title;
		SString descrip;
		SString test;
		int year;
		char plus[10];
		
		in >> artist;
		in >> title;
		in >> year;
		in.getline(plus,10);
		in >> test;
		
		while(1)
		{
			
			if (test.str_compare("END\n") == 0)	
			{	
				break;
			}
			
			SString new_line("\n",1);
			descrip + test;
			descrip + new_line;
			in >> test;
		}
		
		cdArray[cdTotal].set_artist(artist);
		cdArray[cdTotal].set_title(title);
		cdArray[cdTotal].set_year(year);
		cdArray[cdTotal].set_descrip(descrip);
		cdTotal = cdTotal + 1;
	}
	
	else
	{
		cout << "Error: Exceeded the maximum of 100 CD's in the collection" << endl;
	}
	
		
}

Dani AI

Generated

The compiler messages all come from trying to call member functions on an element of a plain char array. As pointed out, the current cdArray is an array of char, so cdArray[index] is a char and has no print/set_* members. The fix is to make cdArray hold the CD object type (or pointers/objects in a container) instead of char.

Suggested changes (minimal, concrete):

// store real CD objects (needs a default ctor)
CD cdArray[100];

// or, better: dynamic container, no manual max/count
std::vector<CD> cdArray;

When adding, create a CD instance, set its fields, then push it into the container:

CD cd;
cd.setArtist(artist);
cd.setTitle(title);
cd.setYear(year);
cd.setDescription(descrip);
cdArray.push_back(cd);

Other bugs to fix while here:

  • The lines that currently just evaluate descrip + test; do not change descrip. Use descrip += test; or descrip = descrip + test; (or append()), and read full lines with std::getline if multi-word descriptions are intended.
  • The sentinel check should compare against "END" (not "END\n") since operator>> won't include the newline.
  • Ensure the CD class (with print, set* methods) is visible to CDCollection (include the header or define CD before CDCollection). If using an array of CD objects, a default constructor is required; std::vector plus emplace_back avoids that requirement.

A quick note about ’s suggestion to “drop the cdArray[index] and just call print(out)”: that would call CDCollection::print recursively (or otherwise be the wrong function), not the per-CD print. get should invoke the CD object's print (or be renamed to printOne), and CDCollection::print should iterate and call that per-CD print.

Recommended Answers

All 3 Replies

>cdArray[num_cds].print(out);
cdArray is an array of char. cdArray[num_cds] is a char. char doesn't define any member functions. I'll let you figure out the rest of the errors, as they're the same thing.

I'm being dense today, because I'm still not getting it.

cdArray[num_cds].print(out);

print() is declared as a member function of the CDCollection class, meaning it should be called using an object of the CDCollection class or be used within the CDCollection class itself without explicit association with a CDCollection object. Unforturnately, in the above line you are trying to call print() using a variable of type char, the specific char being cdArray[num_cds]. However, print() won't work with an object of type char. It will only work with an object of type CDCollection. So, drop the cdArray[num_cds] and just call print(out).

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.