473,414 Members | 1,750 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,414 software developers and data experts.

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 2006
al********@gmail.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********@gmail.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<WeightedSet<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.rWeight;
}

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

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

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(WeightedSet<W>));
}
template <class W>
solution<W> MWSCP<W>::solveGreedy(){

// 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.collections.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>::solveLayered(){
// 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<WeightedSet<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_back(c);
if (mincost == 0 /*the first execution*/ || mincost > cost
)
{mincost = cost; collection = i; coeff = cost/notcovered;
SetCover.clear(); SetCover.push_back(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.collections.push_back(c);
}
// HERE
//list<WeightedSet<float> >::iterator iter;
list<WeightedSet<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<WeightedSet<W> >::iterator iter;
by
list<WeightedSet<float> >::iterator iter;

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

Mar 6 '06 #6
andreil wrote:
template <class W>
solution<W> MWSCP<W>::solveLayered(){
<snip>
list<WeightedSet<W> >::iterator iter;


The type of the iterator is dependant on the template type W, therefore,
try:
typename list<WeightedSet<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
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...
4
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"...
22
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
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
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,...
0
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...

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.