D
deancoo
Hello all, I'm having a problem compiling some code which I believe should
compile. Can someone tell me what I'm doing wrong. The code in question
is appended below, followed by the compiler output.
Thanks,
d
PS: uncommenting the commented out code allows the program to compile.
------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class dummy_obj {
public:
int test_int;
string test_string;
void say_it() {
cout << test_string << endl;
};
};
class dummy_obj_a : public dummy_obj {
public:
dummy_obj_a() {
test_int = 0;
test_string = "Hello Joe";
};
};
class dummy_obj_b : public dummy_obj {
public:
dummy_obj_b() {
test_int = 0;
test_string = "Goodbye Joe";
};
};
int main(int argc, char *argv[])
{
vector<dummy_obj*> my_objs;
char x = 'a';
// dummy_obj_a *obj = new dummy_obj_a;
if (x == 'a') {
dummy_obj_a *obj = new dummy_obj_a;
} else if (x == 'b') {
dummy_obj_b *obj = new dummy_obj_b;
};
my_objs.push_back(obj);
for (vector<dummy_obj*>::iterator z=my_objs.begin(); z!=my_objs.end();
z++) {
(*z)->say_it();
};
system("PAUSE");
return EXIT_SUCCESS;
}
-----------------------------------------------------------
Compiler: Default compiler
Building Makefile: "M:\A\cpp_dev\test\Makefile.win"
Executing make...
make.exe -f "M:\A\cpp_dev\test\Makefile.win" all
g++.exe -c ptr_object_definition_test.cpp -o
ptr_object_definition_test.o -I"M:/A/cpp_dev/poker/include" -I"lib/gcc/mingw32/3.4.2/include"
-I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2"
-I"include" -g3 -O0
ptr_object_definition_test.cpp: In function `int main(int, char**)':
ptr_object_definition_test.cpp:48: error: `obj' undeclared (first use this
function)
ptr_object_definition_test.cpp:48: error: (Each undeclared identifier is
reported only once for each function it appears in.)
make.exe: *** [ptr_object_definition_test.o] Error 1
Execution terminated
compile. Can someone tell me what I'm doing wrong. The code in question
is appended below, followed by the compiler output.
Thanks,
d
PS: uncommenting the commented out code allows the program to compile.
------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class dummy_obj {
public:
int test_int;
string test_string;
void say_it() {
cout << test_string << endl;
};
};
class dummy_obj_a : public dummy_obj {
public:
dummy_obj_a() {
test_int = 0;
test_string = "Hello Joe";
};
};
class dummy_obj_b : public dummy_obj {
public:
dummy_obj_b() {
test_int = 0;
test_string = "Goodbye Joe";
};
};
int main(int argc, char *argv[])
{
vector<dummy_obj*> my_objs;
char x = 'a';
// dummy_obj_a *obj = new dummy_obj_a;
if (x == 'a') {
dummy_obj_a *obj = new dummy_obj_a;
} else if (x == 'b') {
dummy_obj_b *obj = new dummy_obj_b;
};
my_objs.push_back(obj);
for (vector<dummy_obj*>::iterator z=my_objs.begin(); z!=my_objs.end();
z++) {
(*z)->say_it();
};
system("PAUSE");
return EXIT_SUCCESS;
}
-----------------------------------------------------------
Compiler: Default compiler
Building Makefile: "M:\A\cpp_dev\test\Makefile.win"
Executing make...
make.exe -f "M:\A\cpp_dev\test\Makefile.win" all
g++.exe -c ptr_object_definition_test.cpp -o
ptr_object_definition_test.o -I"M:/A/cpp_dev/poker/include" -I"lib/gcc/mingw32/3.4.2/include"
-I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2"
-I"include" -g3 -O0
ptr_object_definition_test.cpp: In function `int main(int, char**)':
ptr_object_definition_test.cpp:48: error: `obj' undeclared (first use this
function)
ptr_object_definition_test.cpp:48: error: (Each undeclared identifier is
reported only once for each function it appears in.)
make.exe: *** [ptr_object_definition_test.o] Error 1
Execution terminated