Solved

Compiling error

Posted on 2003-11-11
4
371 Views
Last Modified: 2010-04-01
Dear All,

I  compiled a project under Unix,  I have got the following errors:

------------------------------------------
 CC -I. -DUNIX -c XPCFileStat.C -g -o XPCFileStat.o
"XPCFileStat.C", line 11: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 20: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 28: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 36: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 48: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 59: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 70: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 77: Error: The "&" operator can only be applied to a variable or other l-value.
"XPCFileStat.C", line 221: Warning: String literal converted to char* in formal argument sMsg in call to XPCException::XPCException(char*).
"XPCFileStat.C", line 228: Error: The "&" operator can only be applied to a variable or other l-value.
2 Error(s) and 8 Warning(s) detected.
*** Error code 2
make: Fatal error: Command failed for target `XPCFileStat.o'
------------------------------------------------------

Here is the main part of the program:

#include <XPCFileStat.h>
#include <iostream.h>

XPCFileStat::XPCFileStat()
{
    long lMaxpath;

    // Determine the maximum size of a pathname
    if ((lMaxpath = pathconf("/", _PC_PATH_MAX)) == -1)
    {
        XPCException newExcept("Could not determine maximum pathname length"); // warning line 11
        throw newExcept;
        return;
    }

    // Allocate memory for the pathname
    cFileName = new char[lMaxpath + 1];
    if (!cFileName)
    {
        XPCException newExcept("Could not allocate memory for cFileName"); // warning line 20
        throw newExcept;
        return;
    }

    // Store the current working directory
    if (getcwd(cFileName, lMaxpath) == NULL)
    {
        XPCException newExcept("Could not get current working directory"); // warning line 28
        throw newExcept;
        return;
    }

    // Retrieve the file's statistics
    if (lstat(cFileName, &sStatBuf) == -1)
    {
        XPCException newExcept("Could not obtain statics on directory."); // warning line 36
        throw newExcept;
        return;
    }
}

XPCFileStat::XPCFileStat(char *_psFileName)
{
    // Allocate memory to store the pathname
    cFileName = new char[strlen(_psFileName)+1];
    if (!cFileName)
    {
        XPCException newExcept("Could not allocate memory for cFileName"); // warming line 48
        throw newExcept;
        return;
    }

    // Copy the pathname to the private data member
    strcpy(cFileName, _psFileName);

    // Retrieve the file's statistics
    if (lstat(cFileName, &sStatBuf) == -1)
    {
        XPCException newExcept("Could not obtain statics on directory."); // warning line 59
        throw newExcept;
        return;
    }    
}

XPCFileStat::XPCFileStat(const XPCFileStat &_oldClass)
{
    cFileName = new char[sizeof(_oldClass.cFileName)+1];
    if (!cFileName)
    {
        XPCException newExcept("Could not allocate memory for cFileName");// warning line 70
        throw newExcept;
        return;
    }

    strcpy(cFileName, _oldClass.sGetFileName());

    memcpy((void *)&sStatBuf, (void *)&_oldClass.getStatBuf(), sizeof(struct stat)); //here is first error
}

enum eDirectoryTypes XPCFileStat::iGetFileType()
{
    // Extract the file type bits from st_mode and match them up with
    // the file type constants

    switch(sStatBuf.st_mode & S_IFMT)
    {
       ..............................
    }
}

enum ePermissions XPCFileStat::iGetOwnerPermissions()
{
    // Extract the owner permission bits and return the appropriate
    // permission value

    switch(sStatBuf.st_mode & S_IRWXU)
    {
        .......................................................
    }
}

enum ePermissions XPCFileStat::iGetGroupPermissions()
{
    // Extract the group permission bits and return the appropriate
        // permission value

    switch(sStatBuf.st_mode & S_IRWXG)
    {
                ..................................................
    }
}

enum ePermissions XPCFileStat::iGetOtherPermissions()
{
    // Extract the "other users" permission bits and return the appropriate
    // permission value

    switch(sStatBuf.st_mode & S_IRWXO)
    {
                ......................................
    }
}

XPCFileStat &XPCFileStat::operator=(const XPCFileStat &_oldClass)
{
    if (this == &_oldClass)
        return *this;

    if (sizeof(cFileName) < sizeof(_oldClass.sGetFileName()))
    {
        delete [] cFileName;
        cFileName = new char[sizeof(cFileName)];
        if (!cFileName)
        {
                    XPCException newExcept("Could not allocate memory for cFileName"); // warning line 221
                    throw newExcept;
                    return *this;
        }    
    }
   
    memcpy((void *)cFileName, (void *)_oldClass.sGetFileName(), sizeof(_oldClass.sGetFileName()));
    memcpy((void *)&sStatBuf, (void *)&_oldClass.getStatBuf(), sizeof(struct stat)); // here is the 2 error
}

=======================================================================

I would be glag if someone would help me to solve at least those two errors. Your help will be appreciated!

Thank you in advance for your reply.
Regards,
Azzedine
0
Comment
Question by:azzedine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 50 total points
ID: 9725113
The error on lines 77 and 228 is because you're trying to get the address of a method, you shouldnt need the & at all, if as I suspect gewtstatbuf returns a pointer.

The other messages are warnings, but why passing a literal to a char * is a warning is beyond me.
Maybe you could post the declarations for that newExcept method?







0
 
LVL 3

Assisted Solution

by:Kashra
Kashra earned 50 total points
ID: 9726778
The warnings come from trying to pass a 'string literal' (which means something you type between quotation marks in your code) to a function that asks for a char *.

This is unsafe because string literals are not supposed to be modified, they are const objects. Therefore they should only be passed to functions that ask for a const char * as their parameter. I'm surprised this is just a warning and not an error message. Either change the function to accept a const char*, or construct the C-style strings ahead of time before passing them to the function.

For your errors:

memcpy((void *)&sStatBuf, (void *)&_oldClass.getStatBuf(), sizeof(struct stat));

If getStatBuf returns a pointer, as noted by grg99, you shouldn't need to use &. But if it returns an object by reference, then try:
memcpy((void *)&sStatBuf, (void *)&(_oldClass.getStatBuf()), sizeof(struct stat));

And likewise with the second error. Good luck!
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249019
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: grg99 {http:#9725113} & Kashra {http:#9726778}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question