views:

48

answers:

3

This is undoubtedly a simple question. I used to do this before, but it's been around 10 years since I worked in C++ so I can't remember properly and I can't get a simple constructor call working.

The idea is that instead of parsing the args in main, main would create an object specifically designed to parse the arguments and return them as required.

So: Parameters params = new Parameters(argc, argv) then I can call things like params.getfile()

Only problem is I'm getting a complier error in Visual Studio 2008 and I'm sure this is simple, but I think my mind is just too rusty.

What I've got so far is really basic:

In the main:

#include "stdafx.h"
#include "Parameters.h"

int _tmain(int argc, _TCHAR* argv[])
{
Parameters params = new Parameters(argc, argv);

return 0;
} 

Then in the Parameters header:

#pragma once

class Parameters
{
 public:
Parameters(int, _TCHAR*[]);
~Parameters(void);
};

Finally in the Parameters class:

include "Stdafx.h"
#include "Parameters.h"

Parameters::Parameters(int argc, _TCHAR* argv[])
{

}

Parameters::~Parameters(void)
{
}

I would appreciate if anyone could see where my ageing mind has missed the really obvious.

Thanks in advance.

+3  A: 

Use:

Parameters params(argc, argv);

The new operator is generally used when you want dynamic (heap) allocation. It returns a pointer (Parameter *).

Matthew Flaschen
+2  A: 

The constructor and the destructor for the Parameters class has to be the same name as the class itself:

// In the header file:
class Parameters 
{ 
public: 
    Parameters(int, _TCHAR*[]); // Not TABOnlineScraperParameters
    ~Parameters(); 
};

// In the source file:
Parameters::Parameters(int, _TCHAR*[])
{
}

Parameters::~Parameters()
{
}

Also, there is no need to call new to create the object:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Parameters params(argc, argv); 
    return 0; 
} 

Just a reminder: the next time you mention a compiler error, it will help us a lot if you provided the exact error output from the compiler.

In silico
A: 

new operator creates an object on the heap. You need a pointer to reference it. Hence, you would use

Parameters * params = new Parameters(argc, argv);

If you want to create a stack variable, do

Parameters params(argc, argv);
Greg Adamski