CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2005
    Posts
    5

    Question C++ program compilation error

    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.

  2. #2
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: C++ program compilation error

    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);
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C++ program compilation error

    Code:
    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:
    Code:
    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).

  4. #4
    Join Date
    May 2005
    Posts
    5

    Re: C++ program compilation error

    Hi Improving,
    Even if i change the call to sprintf, still the problem persists.

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: C++ program compilation error

    Although its not relavent to your original problem, but the return type of main() should be int.

  6. #6
    Join Date
    Sep 2004
    Location
    cache software
    Posts
    106

    Re: C++ program compilation error

    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 !
    __________________________________
    s.m.a.r.t+s.m.a.r.t = ?

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C++ program compilation error

    Quote Originally Posted by rantech
    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.

  8. #8
    Join Date
    Sep 2004
    Location
    cache software
    Posts
    106

    Re: C++ program compilation error

    Quote Originally Posted by NMTop40
    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 !
    __________________________________
    s.m.a.r.t+s.m.a.r.t = ?

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: C++ program compilation error

    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured