42

I have below class

class Cdata12Mnt
{
public:
    char IOBname[ID1_IOB_PIOTSUP-ID1_IOB_TOP][BOADNAM_MAX + 4];
    char ExIOBname[ID1_MAX_INF-ID1_EXIOB_U1TOP][BOADNAM_MAX + 4];
    char cflpath[256];
    char basetext[256];
    UINT database[ID1_MAX_INF];
    int State;

public:
    char SelectPath[256];

public:
    int GetIOBName(int slt,char *Name);
    Cdata12Mnt(char *SelectPath);
    virtual ~Cdata12Mnt();
    int     GetValue(int id);
    int     GetState() { return State; }
};

And I have function as below

Cdata12Mnt::Cdata12Mnt(char *SelectPath)
{
    SCTReg  reg;
    char    buf[256], *cpnt, *npnt, *bpnt1, *bpnt2;
    char    *startcode[] = {"CNTL_CODE ","SEGMENT "};
    char    *stopcode    = {"END_CNTL_CODE "};
    FILE    *fp;
    int     ii, infl;

    State = 0;

    for (ii = 0; ii < (ID1_IOB_PIOTSUP - ID1_IOB_TOP); ii++) {
        strcpy(IOBname[ii], "");
    }

    for (ii = 0; ii < (ID1_MAX_INF-ID1_EXIOB_U1TOP); ii++) {
        **strcpy(ExIOBname[ii], "");**
    }

    sprintf(cflpath, "%s\\%s", SelectPath, CDATAFL);

    if ((fp = fopen(cflpath,"r"))!=NULL) {
        for (ii = 0, infl = 0; fgets(buf, 256, fp) != NULL;) {
            if (infl == 0 && strncmp(buf, startcode[0], strlen(startcode[0])) == 0) {
                if ((cpnt = strchr(&buf[strlen(startcode[0])],*startcode[1])) != NULL) {
                    if (strncmp(cpnt,startcode[1], strlen(startcode[1])) == 0) {
                        infl = 1;
                        continue;
                    }
                }
            }

            if (infl == 0) {
                continue;
            }

            if (strncmp(buf,stopcode,strlen(stopcode))==0) {
                if (ii == ID1_EXIOB_U1TOP) {
                    for (int nDataNumber = ii; nDataNumber < ID1_MAX_INF; nDataNumber++) {
                        database[nDataNumber] = 0;
                    }
                }

                infl = 0;
                continue;
            }

            if (strncmp(&buf[14], " DD ", 4) == 0) {
                if ((cpnt=strchr(buf, ';')) != NULL) {
                    *cpnt = '\0';
                }

                if (ii >= ID1_IOB_TOP && ii < ID1_IOB_PIOTSUP) {
                    if ((bpnt1 = strchr(cpnt + 1,'(')) != NULL && (bpnt2=strchr(cpnt + 1,')'))!=NULL && bpnt1 < bpnt2) {
                        *bpnt2 = '\0';
                        *(bpnt1 + BOADNAM_MAX + 1) = '\0';
                        strcpy(IOBname[ii-ID1_IOB_TOP], bpnt1 + 1);
                    }
                }

                if (ii >= ID1_EXIOB_U1TOP && ii < ID1_MAX_INF) {
                    if ((bpnt1 = strchr(cpnt + 1, '(')) != NULL && (bpnt2=strchr(cpnt+1,')'))!=NULL && bpnt1 < bpnt2) {
                            *bpnt2='\0';
                            *(bpnt1+BOADNAM_MAX+1)='\0';
                            strcpy(ExIOBname[ii-ID1_EXIOB_U1TOP], bpnt1 + 1);
                    }
                }

                for (cpnt = &buf[18]; cpnt != NULL;) {
                    if ((npnt=strchr(cpnt, ',')) != NULL)
                        *npnt='\0';
                }

                if (strchr(cpnt,'H')!=NULL) {
                    sscanf(cpnt,"%XH",&database[ii]);
                } else {
                    database[ii]=atoi(cpnt);
                }

                ii++;
                cpnt = npnt;

                if (cpnt != NULL) {
                    cpnt++;
                }
            }
        }
    }

    fclose(fp);
} else {
    State=-1;
}

When I compile this function in Visual studio 2008, it gives me error at strcpy(IOBname[ii],""); as below

error C2220: warning treated as error - no 'object' file generated

How to fix this error?

Tejendra
  • 1,874
  • 1
  • 20
  • 32
TrungLuu
  • 595
  • 3
  • 8
  • 9
  • 6
    Your project settings contains a flag that tells the compiler to treat warnings as errors. Turn that flag off and you will only have the original warning (whatever that is). – Some programmer dude Aug 14 '13 at 07:33
  • 2
    And by the way, why do you have old C-style string in a C++ project? Use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string), it will work out much better in the long run. Also, instead of old C-style arrays, use [`std::array`](http://en.cppreference.com/w/cpp/container/array) (or [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)). – Some programmer dude Aug 14 '13 at 07:34
  • 3
    Fix your warnings and it will build – paulm Feb 03 '14 at 13:31

5 Answers5

43

The error says that a warning was treated as an error, therefore your problem is a warning message! The object file is then not created because there was an error. So you need to check your warnings and fix them.

In case you don't know how to find them: Open the Error List (View > Error List) and click on Warning.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Nym Anno
  • 471
  • 4
  • 2
37

Go to project properties -> configurations properties -> C/C++ -> treats warning as error -> No (/WX-).

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • 47
    So your solution is : "blindly ignore all warnings" ? – ereOn Jan 16 '15 at 15:59
  • 13
    @mojtaba setoodeh, ignoring warnings is not a fix, but a bypass. – TheFrancisOne Jan 28 '15 at 08:20
  • 9
    Not a valid solution! – vishal Mar 16 '17 at 12:33
  • 12
    It depends on what you are trying to do. If you are working on a shared code base and the modules causing this error aren't of concern to you, this solution should get you going so that you can focus on what you are doing. – shaveenk Apr 03 '19 at 19:57
  • 4
    Why do you care in development mode. just disable as suggested and focus on work. Later enable it again, fix the warnings before checking in. – milanchandna Apr 15 '19 at 05:13
  • Not valid answer – maidamai Sep 07 '19 at 02:34
  • 1
    It also fails on warnings like C4101, unreferenced local variable. Everyone may have a different opinion about that, but I don't like that at all, especially during development phase. – Hneel Jun 18 '21 at 13:41
  • 1
    "blindly ignore all warnings" is a good solution especially when one need to deal with a legacy projects – user3124812 Nov 23 '21 at 23:18
  • Compiling legacy & 3rd party code may necessitate ignoring warnings in some way. Ideally disable them by specific warning number. One of the answers shows how with pragmas; warnings can also be disabled in Visual Studio via `Properties > C/C++ > Advanced > Disable Specific Warnings`. – AbePralle Sep 12 '22 at 07:34
  • I'm actually fine with the approach to this answer, at least for certain situations. However in my case, it's already set to "no." -1. – CoryCoolguy Jan 25 '23 at 05:17
9

As a side-note, you can enable/disable individual warnings using #pragma. You can have a look at the documentation here

From the documentation:

// pragma_warning.cpp
// compile with: /W1
#pragma warning(disable:4700)
void Test() {
   int x;
   int y = x;   // no C4700 here
   #pragma warning(default:4700)   // C4700 enabled after Test ends
}

int main() {
   int x;
   int y = x;   // C4700
}
ButterDog
  • 5,115
  • 6
  • 43
  • 61
8

This error message is very confusing. I just fixed the other 'warnings' in my project and I really had only one (simple one):

warning C4101: 'i': unreferenced local variable

After I commented this unused i, and compiled it, the other error went away.

zar
  • 11,361
  • 14
  • 96
  • 178
  • 4
    I confirm that, i had a similar issue today. Had an "Unreachable code" warning somewhere plus this confusing "no 'object file generated'". Fixing the unreachable warning also eliminated the second one. Kind of strange. – Wurmloch Jul 07 '16 at 09:49
2

This warning is about unsafe use of strcpy. Try IOBname[ii]='\0'; instead.

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54