Error: "Identifier "art" is undefined

How do I remove the error and output appropriately like the following?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Output: ==========================

ID: 12345
Title: The Kiss
Artist: Gustav Klimt
Paint Medium: Oil
Genre: Symbolist
Year: 1908
Price: $2500

ID: 54321
Title: The Thinker
Artist: Rodin
Material: Bronze
Genre: Impressionism
Year: 1880
Price: $2000
===================================== */



Here's my main.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 
#include "Art.h"
#include "Painting.h"
#include "Sculpture.h"

using namespace std;

int main()
{
	Painting a1("12345", "The Kiss", "Gustav Klimt", "Symbolist", 1908, 2500, "Oil");
	Sculpture a2("54321", "The Thinker", "Rodin", "Impressionism", 1880, 2000, "Bronze");

	a1.showArt();
	a2.showArt();

	void displayArt(Art & art);
	{
		art.showArt();
	}
}


I'm receiving the error on art.showArt();

Here are the other files:

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
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 

#ifndef ART_H
#define ART_H

using namespace std;

class Art {
protected:
	string id;
	string title;
	string artist;
	string genre;
	int year;
	double price;
public: 
	Art();
	Art(string id, string title, string artist, string genre, int year, double price);
	~Art();
	virtual void showArt() = 0;

};
#endif // !ART_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 
#include"Art.h"

#ifndef PAINTING_H
#define PAINTING_H

using namespace std;

class Painting : public Art {
private:
	string paintMedium;
public:
	Painting(string id, string title, string artist, string genre, int year, double price, string paintMedium);
	~Painting();
	void showArt();
};
#endif // !PAINTING_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 
#include"Art.h"

#ifndef SCULPTURE_H
#define SCULPTURE_H

using namespace std;

class Sculpture : public Art {
private: 
	string material;
public:
	Sculpture(string id, string title, string artist, string genre, int year, double price, string material);
	~Sculpture();
	void showArt();

};
#endif // !SCULPTURE_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 
#include "Art.h"

using namespace std;

Art::Art() {}

Art::Art(string id, string title, string artist, string genre, int year, double price)
{
	this->id = id;
	this->title = title;
	this->artist = artist;
	this->genre = genre;
	this->year = year;
	this->price = price;
}

Art::~Art() {}

void Art::showArt() {} //how do we flesh out a pure virtual function in the .cpp file? 


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
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 
#include "Painting.h"

using namespace std;

Painting::Painting(string id, string title, string artist, string genre, int year, double price, string paintMedium)
{
	this->id = id;
	this->title = title;
	this->artist = artist;
	this->genre = genre;
	this->year = year;
	this->price = price;
	this->paintMedium = paintMedium;
}

void Painting::showArt()
{
	cout << "ID: " << id << endl;
	cout << "Title: " << title << endl;
	cout << "Artist: " << artist << endl;
	cout << "Paint Medium: " << paintMedium << endl;
	cout << "Genre: " << genre << endl;
	cout << "Year: " << year << endl;
	cout << "Price: " << price << endl;
}

Painting::~Painting() {}


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
#pragma once
#include<iostream>
#include<iomanip> 
#include <string> 
#include "Sculpture.h"

using namespace std;

Sculpture::Sculpture(string id, string title, string artist, string genre, int year, double price, string material)
{
	this->id = id;
	this->title = title;
	this->artist = artist;
	this->genre = genre;
	this->year = year;
	this->price = price;
	this->material = material;
}

void Sculpture::showArt()
{
	cout << "ID: " << id << endl;
	cout << "Title: " << title << endl;
	cout << "Artist: " << artist << endl;
	cout << "Material: " << material << endl;
	cout << "Genre: " << genre << endl;
	cout << "Year: " << year << endl;
	cout << "Price: " << price << endl;
}

Sculpture::~Sculpture() {}
Last edited on
You cannot define functions within other functions like that.
Here are the instructions from the homework assignment:

4.) In main(), declare a function named: displayArt

- Here is the function prototype: void displayArt(Art & art);

• Note: A base class object (Art) is passed to the function. However, because Painting and
Sculpture objects are also Art objects, objects of either class can be passed to
the function.

• Here is the function in main.cpp

void displayArt(Art & art)
{
art.showArt();
}

Last edited on
I put

void displayArt(Art & art)
{
art.showArt();
}

OUTSIDE of the main as a standalone function, and this worked. Thanks Peter.
You need to move displayArt() outside of main. You can't define a function within another function:
1
2
3
4
5
6
7
8
9
10
11
12
13
void displayArt(Art & art);
{
	art.showArt();
}

int main()
{
	Painting a1("12345", "The Kiss", "Gustav Klimt", "Symbolist", 1908, 2500, "Oil");
	Sculpture a2("54321", "The Thinker", "Rodin", "Impressionism", 1880, 2000, "Bronze");

	a1.showArt();
	a2.showArt();
}


Unfortunately, the way you wrote it is nearly-valid syntax. Here's what it actually means and why you got the error that you did:
1
2
3
4
5
6
7
8
int main()
{
	...
	void displayArt(Art & art);  // declares an extern function called displayArt
	{	// starts a new block within main
		art.showArt();	// calls art.showArt. But art is undefined within main
	}	// ends the block started above.
}
Topic archived. No new replies allowed.