Error with the following code

M

Materialised

I am having some issues with the following code, basically what I am
trying to do, is familierise myself with compiler warnings, when I try
to access a private or protected class function.
Here is the errors the compiler generates:

ex1.cpp: In function `int main()':
ex1.cpp:12: error: `testing' is not a class or namespace
ex1.cpp:12: error: `pubtest' undeclared (first use this function)
ex1.cpp:12: error: (Each undeclared identifier is reported only once for
each function it appears in.)
ex1.cpp:13: error: `testing' is not a class or namespace
ex1.cpp:13: error: `pritest' undeclared (first use this function)
ex1.cpp:14: error: `testing' is not a class or namespace
ex1.cpp:14: error: `protest' undeclared (first use this function)
test.cpp:1:24: ex1-Header.h: No such file or directory
test.cpp:4: error: `Test' has not been declared
test.cpp:9: error: `Test' has not been declared
test.cpp:14: error: `Test' has not been declared


Here is the code:
###ex1-Header.h
class Test {
public:
void pubtest();
private:
void pritest();
protected:
void protest();
};

###test.cpp

#include <ex1-Header.h>
#include <iostream>

void Test::pubtest() {
std::cout << "Public Test" << std::endl;
}
void Test::pritest() {
std::cout << "Private Test" << std::endl;
}
void Test::protest() {
std::cout << "Protected Test" << std::endl;
}

###ex1.cpp
#include "ex1-Header.h"
#include <iostream>

using namespace std;

int main() {
Test testing;
testing::pubtest();
testing::pritest();
testing::protest();

return 0;
}

I did expect compiler errors when trying to access the private and
protected functions, however even when I comment out the calls to these
functions the compiler still issues warnings.
Can anyone point me in the correct direction of where I am going wrong.
Thanks


--
Materialised
perl -e 'printf %silto%c%sal%c%s%ccodegurus%corg%c, ma, 58, mw, 107,
'er', 64, 46, 10;'


Bart: "What if you're a really good person, but you get into a really,
really bad fight and your leg gets gangrene and it has to be amputated.
Will it be waiting for you in heaven?"
 
S

Serge Paccalin

Le lundi 2 mai 2005 à 13:20:50, Materialised a écrit dans
comp.lang.c++ :
I am having some issues with the following code, basically what I am
trying to do, is familierise myself with compiler warnings, when I try
to access a private or protected class function.
Here is the errors the compiler generates:

ex1.cpp: In function `int main()':
ex1.cpp:12: error: `testing' is not a class or namespace
[snip]

Test testing;
testing::pubtest();
testing::pritest();
testing::protest();

return 0;
}

I did expect compiler errors when trying to access the private and
protected functions, however even when I comment out the calls to these
functions the compiler still issues warnings.
Can anyone point me in the correct direction of where I am going wrong.
Thanks

The compilder did: `testing' is not a class or namespace. '::' applies
to a class or a namespace, not an object.

testing.pubtest();
testing.pritest();
testing.protest();

--
___________ 02/05/2005 13:25:10
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
M

Materialised

Materialised said:
I am having some issues with the following code, basically what I am
trying to do, is familierise myself with compiler warnings, when I try
to access a private or protected class function.
Here is the errors the compiler generates:

ex1.cpp: In function `int main()':
ex1.cpp:12: error: `testing' is not a class or namespace
ex1.cpp:12: error: `pubtest' undeclared (first use this function)
ex1.cpp:12: error: (Each undeclared identifier is reported only once for
each function it appears in.)
ex1.cpp:13: error: `testing' is not a class or namespace
ex1.cpp:13: error: `pritest' undeclared (first use this function)
ex1.cpp:14: error: `testing' is not a class or namespace
ex1.cpp:14: error: `protest' undeclared (first use this function)
test.cpp:1:24: ex1-Header.h: No such file or directory
test.cpp:4: error: `Test' has not been declared
test.cpp:9: error: `Test' has not been declared
test.cpp:14: error: `Test' has not been declared


Here is the code:
###ex1-Header.h
class Test {
public:
void pubtest();
private:
void pritest();
protected:
void protest();
};

###test.cpp

#include <ex1-Header.h>
#include <iostream>

void Test::pubtest() {
std::cout << "Public Test" << std::endl;
}
void Test::pritest() {
std::cout << "Private Test" << std::endl;
}
void Test::protest() {
std::cout << "Protected Test" << std::endl;
}

###ex1.cpp
#include "ex1-Header.h"
#include <iostream>

using namespace std;

int main() {
Test testing;
testing::pubtest();
testing::pritest();
testing::protest();

return 0;
}

I did expect compiler errors when trying to access the private and
protected functions, however even when I comment out the calls to these
functions the compiler still issues warnings.
Can anyone point me in the correct direction of where I am going wrong.
Thanks
My bad, I should have been using object.method();
Thanks Anyway.

--
Materialised
perl -e 'printf %silto%c%sal%c%s%ccodegurus%corg%c, ma, 58, mw, 107,
'er', 64, 46, 10;'


Homer: "Ha ha! Look at this country! ? R U Gay!? Ha ha!" (looking at
Uruguay on the globe)
 
M

MJ

Hi
Nothing you have to access the object using the . operatore instead of
scope resolution operator

check it below
#include <iostream>

using namespace std;
class Test {
public:
void pubtest();
private:
void pritest();
protected:
void protest();

};

void Test::pubtest() {
std::cout << "Public Test" << std::endl;
}

void Test::pritest() {
std::cout << "Private Test" << std::endl;
}

void Test::protest() {
std::cout << "Protected Test" << std::endl;

}


int main() {
Test testing;
testing.pubtest();
//testing::pubtest();
// testing.pritest();
// testing::protest();

return 0;

}

MJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top