Click to See Complete Forum and Search --> : C++ program compilation error


mbathala
May 20th, 2005, 06:58 AM
I have written a class to append a location to 'PATH' environment variable. When i try to compile the code, i'm getting error as
"error C2040: 'clientDllPath' : 'class kkAddToPath' differs in levels of indirection from 'char *'". Is there any error in this code?

class kkAddToPath {
public:
kkAddToPath() {
}
kkAddToPath(char *Path) {
char *curPath = getenv("PATH");
m_oldPath = new char[strlen(curPath) + 10];
strcpy(m_oldPath, curPath);
char *newPath = new char[strlen(curPath) + strlen(Path) + 20];
sprintf(newPath, "%s%s%s%s", "PATH=", Path, ";", m_oldPath);
putenv(newPath);
delete [] newPath;
}

~kkAddToPath() {
char *oldPath = new char[strlen(m_oldPath) + 10];
sprintf(oldPath, "%s%s", "PATH=", m_oldPath);
putenv(oldPath);
delete [] oldPath;
delete [] m_oldPath;
}
private:
char *m_oldPath;
};

void main(int argc, char* argv[])
{
char *path = argv[1];
kkAddToPath(path);
}

Thanks
BM.

Improving
May 20th, 2005, 07:11 AM
Firstly use code tags to post code. It makes it way easier for us to go through if you do.
Secondly main always returns an int. A good compiler should puke on void main.It is not standard and never was.
Thirdly your sprintf calls are way off.

try this
sprintf(newPath, "PATH=%s;%s", Path,m_oldPath);
sprintf(oldPath, "PATH=%s", m_oldPath);

NMTop40
May 20th, 2005, 07:20 AM
kkAddToPath(path);


Is this VC6? It seems to have problems creating temps that way. Technically that line should create a temp.

Anyway, if you do this:

kkAddToPath k( path );

it may make your compiler happy.

There are other issues about using string and const etc. but then getenv has problems with const and string. (One of those parts they should fix and haven't got round to yet).

mbathala
May 20th, 2005, 07:22 AM
Hi Improving,
Even if i change the call to sprintf, still the problem persists.

Ejaz
May 20th, 2005, 07:23 AM
Although its not relavent to your original problem, but the return type of main() should be int.

rantech
May 20th, 2005, 09:51 AM
You are not constructing the object !
You're trying to call a constructor which is wrong !

Follow NMTop40 advice .
kkAddToPath k(path);

Look in books for the structure and functionality of classes !

NMTop40
May 20th, 2005, 10:02 AM
You are not constructing the object !
You're trying to call a constructor which is wrong !

Follow NMTop40 advice .
kkAddToPath k(path);

Look in books for the structure and functionality of classes !

Is it wrong? Calling constructor( parameters ) should create a temp. Just that VC6 seems to get confused.

rantech
May 20th, 2005, 10:38 AM
Is it wrong? Calling constructor( parameters ) should create a temp. Just that VC6 seems to get confused.

In this context it's obvious that this is wrong . I think you're referr at the case of constructing an object in a dynamic way (with new )! Enlight me if you think to something else !

Graham
May 20th, 2005, 12:04 PM
It could be a valid thing to do, depending on what happens in the ctor and dtor. In this case, the ctor adds a directory to the path, and the dtor removes it, so it's a bit pointless, but one could come up with a situation where there's some useful work done (rare, I admit, but possible).

No-one has yet pointed out that the dtor could throw an exception...

...and, of course, the whole thing could be greatly simplified by using std::string.