I'm trying to write a class called "state" and I'm trouble with defining one of the constructors using variable argument list.
The "state" class has four members:

class state{
    nucleon species; 
    vector<int> particleLevel;
    vector<halfint> intermediateAm;
    halfint totalAm;
      public:
        state(nucleon, ...);
      };

"nucleon" is an enum type defined as:

enum nucleon {proton, neutron};

"halfint" is a type i defined by constructing a "halfint" class. "halfint" is half-integers, which can be 1/2, 1, 3/2, etc. A typical "state" object would have it's species defined, some elements in both of the two vectors, and a "totalAm". I'm trying to define a constructor for "state" such that a user can construct a state object using variable arguments. For example:

state ket(proton, 1, 2, 1/2, 1/2);

Here, "1, 2" will be in "particleLevel", the first "1/2" is in "intermediateAm", and the second "1/2" is the "totalAm". I'm having trouble since "halfint" is a non-POD type, so I can only use pointers to halfint. Meanwhile, I also have int in the argument list. The presences of both types force me to use void pointers "void". In principle, I would take in all the variables in the argument list as void pointers, and put them into a vector of void pointers, then indicate somewhere in my constructor which ones are halfint and which ones are int*, then push them into the two member vectors. However, I'm running into trouble. The following is part of my definition of the constructor:

state :: state (nucleon type, ...){
    species = type;
    va_list ap;
    va_start (ap, type);
    int count = 0;[QUOTE]This count is here for debugging purposes[/QUOTE]
    vector<void*> pointer_list;
    void* vp;
    while ((vp = va_arg(ap, void*))!= NULL) {
        vp = va_arg(ap, void*);
        pointer_list.push_back(vp);
        count ++;
        cout << count;
    }
    va_end(ap);
        }

In main, I tried to contruct a state ket1 as:

int main (int argc, char * const argv[]) {
    halfint a(1,2), b(1), c(3/2), d(2), e(5/2), f(3);
    int n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5;
        state ket1(proton, &n1, &n2, &a);
        return 0;
        }

The program compiled ok, and was executed. However, in my terminal, I see "12345", which is printed by cout<<count;, indicating that the while loop has been executed 5 times, when I only have three un-named arguments in my variable list. I tried declaring several state objects using this constructor and the while loop is always executed a weird number of times. Here are some examples:

  state ket2(proton, &n1, &n2, &a, &b)  //while loop executed 5 times
  state ket3(proton, &n1, &n2, &a, &b, &c); // 6 times
  state ket2(proton, &n1, &n2, &n3, &a, &b, &c); // 6 times
  state ket2(proton, &n1);  // 5 times
  state ket2(proton, &a);   // 5 times
  state ket2(proton);  //5 times

So it seems like the while loop is always executed 5 times when the number of arguments don't exceed5, then it's executed one additional time when one or two more arguments are added. What is wrong with this? I suspect that the while loop condition has problem, but i don't know what problem it is...
Can anyone give me some idea?

Sorry, I'm new to posting on forums, and I messed up something, the definition of class "state" should be :

state :: state (nucleon type, ...){
	species = type;
	va_list ap;
	va_start (ap, type);
	int count = 0;
	vector<void*> pointer_list;
	void* vp;
	while ((vp = va_arg(ap, void*))!= NULL) {
		vp = va_arg(ap, void*);
		pointer_list.push_back(vp);
		count ++;
		cout << count;
	}
	va_end(ap);
        }

Sorry if you find it confusing in the original post.

Sorry again, I just realized that I moved ap twice in a row, which is wrong. But fixing that, the while loop is always executed 3 times without any argument in the list...

state :: state (nucleon type, ...){
	species = type;
	va_list ap;
	va_start (ap, type);
	int count = 0;
	vector<void*> pointer_list;
	void* vp;
	while ((vp = va_arg(ap, void*))!= NULL) {
		pointer_list.push_back(vp);
		count ++;
		cout << count;
	}
	va_end(ap);
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.