473,597 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list<WS<W> >

I have a template class

template <Class W>
class WS
[skipped]
At some point I have to use a STL list WS<W>objects
so, I define
#include <list>
using std;
[skipped]
list<WS<W> > sets;

fine

but if try to create an iterator

list<WS<W> >::iterator iter;
then g++ does not compile it

I get a message error: expexted ";" before iter

Why?

Mar 6 '06 #1
9 2016
al********@gmai l.com writes:

....
I get a message error: expexted ";" before iter

Why?


Post a complete non-working code snippet and we'll figure it out.

S.
Mar 6 '06 #2
TB
al********@gmai l.com skrev:
I have a template class

template <Class W>
class WS
[skipped]
At some point I have to use a STL list WS<W>objects
so, I define
#include <list>
using std;
[skipped]
list<WS<W> > sets;

fine

but if try to create an iterator

list<WS<W> >::iterator iter;
then g++ does not compile it

I get a message error: expexted ";" before iter

Why?


And what is 'W'? Is it a class? Or did you just
write 'W' because you named the template argument 'W'?

--
TB @ SWEDEN
Mar 6 '06 #3
// ---------- MWSCP.h--------------------------
template <class W>
class WeightedSet {
public:
WeightedSet(W, int);
W weight;
W rweight;
int els;
int *elements;
bool operator <(const WeightedSet<W>& );
bool operator ==(const WeightedSet<W>& );
bool operator >(const WeightedSet<W>& );
};
template <class W>
struct solution {
W cost;
int size;
list<WeightedSe t<W> > collections;
};

template <class W>
class MWSCP {
public:
MWSCP(int pels, int pcolls);
int els; // the total number of elements
int colls; //the total number of collections
int* covered; //covered[element]=0 if element is covered by a
current choice of collections
WeightedSet<W> *collections; // collections
solution<W> solveGreedy();
solution<W> solveLayered();

bool check();
};

//---------------------------------MWSCP.cpp------------------------------------
#include <list>
using namespace std;

template <class W>
WeightedSet<W>: :WeightedSet(W w, int n) {
// w is the weight, n is the number of elements
weight = w;
els = n;
elements = new int[n];
}

template <class W>
bool WeightedSet<W>: :operator <(const WeightedSet<W>& sOperand)
{
return this.rweight < sOperand.rWeigh t;
}

template <class W>
bool WeightedSet<W>: :operator >(const WeightedSet<W>& sOperand)
{
return this.rweight > sOperand.rWeigh t;
}

template <class W>
bool WeightedSet<W>: :operator ==(const WeightedSet<W>& sOperand)
{
return this.rweight == sOperand.rWeigh t;
}

template <class W>
MWSCP<W>::MWSCP (int pels, int pcolls) {
// pels is the total number of elements, pcolls is the number of
collections
els = pels;
covered = new int[els];
for (int i=0; i < els; i++) covered[i] = 0;
colls = pcolls;
collections = (WeightedSet<W> *)malloc(colls *
sizeof(Weighted Set<W>));
}
template <class W>
solution<W> MWSCP<W>::solve Greedy(){

// solves this instance of the MWSCP
// heap-ified version is defined in MWSCPHeap
W totalcost = 0; int executions = 0;
solution<W> solution;
while (!check() && executions++ < els) {
// choose the lowers price
W mincost = 0;
int collection = 0;
for (int i = 0; i< colls; i++ ){
// check all collections to find a collection with the
minimal cost per uncovered element
W cost;
int notcovered = 0;
WeightedSet<W> c = collections[i];
for (int j = 0; j < c.els; j++) if
(covered[c.elements[j]] == 0 ) notcovered++;
if (notcovered > 0) cost =
c.weight/(static_cast<W> (notcovered));
else continue; // all elements of a given collection are
covered already
if (mincost == 0 /*the first execution*/ || mincost > cost
) {mincost = cost; collection = i; continue;}
}
//int x;
WeightedSet<W> c = collections[collection];
//cout << collection; cout.flush();
totalcost += c.weight;
for (int j = 0; j < c.els; j++) covered[c.elements[j]] = 1;
solution.collec tions.push_back (c);
for (int k =0; k < els; k++) cout << covered[k]; cout <<
endl;cout.flush ();
//cin >> x;
}
solution.size = executions+1;
solution.cost = totalcost;
return solution;
}

template <class W>
solution<W> MWSCP<W>::solve Layered(){
// Heap-fied version is defiend in MWSCPHeap
W residue[colls]; // coefficient of the large-degree function
W coeff; // coefficient of the largest-degree function
solution<W> solution;
for (int i=0; i < colls; i++) residue[i] = collections[i].weight;
list<WeightedSe t<W> > SetCover; // a list of collections which will
be chosen at this layer
W totalcost = 0; int executions = 0;
while (!check() && executions++ < els) {
// choose the lowers price
W mincost = 0;
int collection = 0;
for (int i = 0; i< colls; i++ ){
// check all collections to find a collection with the
minimal cost per uncovered element
W cost;
int notcovered = 0;
WeightedSet<W> c = collections[i];
for (int j = 0; j < c.els; j++) if
(covered[c.elements[j]] == 0 ) notcovered++;
if (notcovered > 0) cost =
(residue[i])/(static_cast<W> (notcovered));
else continue; // all elements of a given collection are
covered already
if (mincost == cost) SetCover.push_b ack(c);
if (mincost == 0 /*the first execution*/ || mincost > cost
)
{mincost = cost; collection = i; coeff = cost/notcovered;
SetCover.clear( ); SetCover.push_b ack(c);
continue;}
}
//int x;
WeightedSet<W> c = collections[collection];
//cout << collection; cout.flush();
// before update we have to compute new residue...it can be
postponed to the next step
for (int i = 0; i < colls; i++){
WeightedSet<W> c = collections[i];
int notcovered = 0;
for (int j = 0; j < c.els; j++) if (covered[c.elements[j]] == 0 )
notcovered++;
residue[i] -= notcovered * coeff;
solution.collec tions.push_back (c);
}
// HERE
//list<WeightedSe t<float> >::iterator iter;
list<WeightedSe t<W> >::iterator iter;
for (iter = SetCover.begin( ); iter != SetCover.end(); iter++){
totalcost += iter->weight;
solution.size +=1;
for (int j = 0; j < iter->els; j++) covered[iter->elements[j]]
= 1;}
//for (int k =0; k < els; k++) cout << covered[k]; cout <<
endl;cout.flush ();
//cin >> x;
for (int k =0; k < els; k++) cout << covered[k]; cout <<
endl;cout.flush ();

}
solution.cost = totalcost;
return solution;
}

template <class W>
bool MWSCP<W>::check () {
// check if all elements are covered
for (int i=0; i<els; i++) if (covered[i] == 0) return 0;
return 1;
}

Mar 6 '06 #4
see below
it is the template argument

Mar 6 '06 #5
PS the problematic line is marked by the word "HERE"
if you replace
list<WeightedSe t<W> >::iterator iter;
by
list<WeightedSe t<float> >::iterator iter;

then it works,
but
I can not compile
list<WeightedSe t<W> >::iterator iter;

Mar 6 '06 #6
andreil wrote:
template <class W>
solution<W> MWSCP<W>::solve Layered(){
<snip>
list<WeightedSe t<W> >::iterator iter;


The type of the iterator is dependant on the template type W, therefore,
try:
typename list<WeightedSe t<float> >::iterator iter;

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 6 '06 #7
andreil wrote:
see below
it is the template argument


To what are you referring? There is nothing below.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 6 '06 #8
Thank you, Ben!
Andrei

Mar 6 '06 #9

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

Similar topics

3
1473
by: Martin | last post by:
Hi Guys, I am a bit of a JavaScript rookie, so please bear with me .... Some time ago, I asked for help with some code to populate a drop down country box based on a value I was returning from a popup menu. This is all now working fine (Thanks !) but I have noticed, it unfortunately does not scale well, and is causing quite some delay between the user hitting 'go', and the country
4
2812
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript" src="country_state.js"> </script>
22
28298
by: Ubi | last post by:
I am looking for a list of countries and nationalities, against the top level domain. es: ..ar ==Argentina ==Argentinian ..al ==Albania ==Albanian etc. etc.
11
17966
by: pinocchio123 | last post by:
hi friends... in my php page 3 dropdownlist boxes are there.. ie country,state,city.. if country is selected, in onchange function.. corresponding states has to be populated in state dropdownlist box.. likewise if particular state is selected corresponding city has to be populated in 3rd list box.. can u help me?? thank you...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6686
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.