473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

inheritance and typedef - compilation error

Suppose the following program is named x.cpp

#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>
{
public:
vector<T>::iterator begin()
{
cout << "non-const begin() is called" << endl;
return vector<T>->begin();
}

vector<T>::const_iterator begin() const
{
cout << "const begin() is called" << endl;
return vector<T>->begin();
}
};

int main()
{
return 0;
}

When I compile this program with g++ as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

I am getting the following compilation errors:

x.cpp:10: error: expected `;' before "begin"
x.cpp:16: error: expected `;' before "vector"
x.cpp:16: error: expected `;' before "begin"
x.cpp:21: error: expected `;' before '}' token

Please help me fix the compilation errors.

Can't we use the base-class iterator and const_iterator types in the
derived class Vec ?

Kindly explain.

Thanks
V.Subramanian

Oct 25 '07 #1
4 1929
On 2007-10-25 10:20, su**************@yahoo.com, India wrote:
Suppose the following program is named x.cpp

#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>
std::vetor<Tdoes not have a virtual destructor and is not designed to
be used as a base class. The usual recommendation is to either use
private inheritance or have a vector as a member instead.
{
public:
vector<T>::iterator begin()
typename vector<T>::iterator begin()

iterator is a dependent type and you need to tell the compiler that it
is a type and nothing else.
{
cout << "non-const begin() is called" << endl;
return vector<T>->begin();
return vector<T>::begin();

The -operator is used when you have a pointer, you have no such thing
in this case.

--
Erik Wikström
Oct 25 '07 #2
su**************@yahoo.com, India wrote:
Suppose the following program is named x.cpp

#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>
{
public:
vector<T>::iterator begin()
{
cout << "non-const begin() is called" << endl;
return vector<T>->begin();
}

vector<T>::const_iterator begin() const
{
cout << "const begin() is called" << endl;
return vector<T>->begin();
}
};

int main()
{
return 0;
}

When I compile this program with g++ as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

I am getting the following compilation errors:

x.cpp:10: error: expected `;' before "begin"
x.cpp:16: error: expected `;' before "vector"
x.cpp:16: error: expected `;' before "begin"
x.cpp:21: error: expected `;' before '}' token

Please help me fix the compilation errors.
#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>
{
public:
typename vector<T>::iterator begin()
{
cout << "non-const begin() is called" << endl;
return vec.begin();
}

typename vector<T>::const_iterator begin() const
{
cout << "const begin() is called" << endl;
return vec.begin();
}
private:
vector<Tvec;
};

int main()
{
return 0;
}
Oct 25 '07 #3
On 2007-10-25 10:54, anon wrote:
su**************@yahoo.com, India wrote:
>Suppose the following program is named x.cpp

#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>
{
public:
vector<T>::iterator begin()
{
cout << "non-const begin() is called" << endl;
return vector<T>->begin();
}

vector<T>::const_iterator begin() const
{
cout << "const begin() is called" << endl;
return vector<T>->begin();
}
};

int main()
{
return 0;
}

When I compile this program with g++ as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

I am getting the following compilation errors:

x.cpp:10: error: expected `;' before "begin"
x.cpp:16: error: expected `;' before "vector"
x.cpp:16: error: expected `;' before "begin"
x.cpp:21: error: expected `;' before '}' token

Please help me fix the compilation errors.

#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>
class Vec // No need to inherit from vector
{
public:
typename vector<T>::iterator begin()
{
cout << "non-const begin() is called" << endl;
return vec.begin();
}

typename vector<T>::const_iterator begin() const
{
cout << "const begin() is called" << endl;
return vec.begin();
}
private:
vector<Tvec;
};

int main()
{
return 0;
}
--
Erik Wikström
Oct 25 '07 #4
Erik Wikström wrote:
On 2007-10-25 10:54, anon wrote:
>su**************@yahoo.com, India wrote:
>>Please help me fix the compilation errors.
#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Vec : public vector<T>

class Vec // No need to inherit from vector
I must admit that I missed that tiny detail. I realized it only after
your reply.
My focused was more on making it compile, because after all that was
what he asked for ;)
Oct 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
0
by: Tseng, Ling-hua | last post by:
As the comments of the following C++ code, I got the ambiguous error in the specialized class " PThreadTimer<void *(*)(void *)> ". I performed the virtual inheritance mechanism on its base classes...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector...
2
by: Søren Holstebroe | last post by:
Hi there, I'm having a struggle with GNU g++ (3.3.5) and inheritance of typedefs in STL containers. I'm trying to port some old code I wrote with MS Visual C++ and it looks like there is a...
1
by: lutorm | last post by:
Hi all, I'm working on migrating my code from KCC to gcc, and I'm having some issues with "implicit typename" warnings from gcc. Essentially, what happens is described by this example: ...
3
by: Doug Eleveld | last post by:
Hi Everyone, I have been playing aroung with 'object-oriented' C for a while now and I have come up with an interesting way to simulate C++ inheritance and non-member functions in C in a 100%...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
4
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.