none
"error LNK2005" RRS feed

  • Question

  • I have the following code in my Form1.h file:

    #include "stdafx.h"

    #include <stdio.h>

    #pragma once

    struct Record {

    char arr[15];

    };

    Record theRecord;

    errno_t err_temp;

    FILE *tempfile;

    namespace StrToCharArr {

    .... //typical stuff

    }

    #pragma endregion

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

    StrToCharArray(this->textBox1->Text);

    }

    };

    }

     

    My StrToCharArray.cpp function is the following:

    #include "stdafx.h"

    #include "Form1.h"

    using namespace StrToCharArr;

    void Form1::StrToCharArray(String^ str) {

    err_temp = tmpfile_s(&tempfile);

    fprintf(tempfile,"%s",str);

    }

    I am getting the follwing error messages:

    1>StrToCharArray.obj : error LNK2005: "struct Record theRecord" (?theRecord@@$$Q3URecord@@A) already defined in StrToCharArr.obj

    1>StrToCharArray.obj : error LNK2005: "int err_temp" (?err_temp@@$$Q3HA) already defined in StrToCharArr.obj

    1>StrToCharArray.obj : error LNK2005: "struct _iobuf * tempfile" (?tempfile@@$$Q3PAU_iobuf@@A) already defined in StrToCharArr.obj

    1>C:\Documents and Settings\hasty\My Documents\Visual Studio 2005\Projects\StrToCharArr\Debug\StrToCharArr.exe : fatal error LNK1169: one or more multiply defined symbols found

    The funny thing is, I don't seem to be getting these messages at all when I don't bring the soucre code in StrToCharArray.cpp and just paste the code directly in the Form1.h. Anybody know whats going on?

    Wednesday, September 13, 2006 7:06 PM

Answers

  • If you have code in *.h files, it needs to be class scope, not just namespace scope.  An alternative is to mark these functions as inline.  Otherwise multiple inclusions will result in multiple definitions.

     

    Wednesday, September 13, 2006 7:14 PM
    Moderator

All replies

  • If you have code in *.h files, it needs to be class scope, not just namespace scope.  An alternative is to mark these functions as inline.  Otherwise multiple inclusions will result in multiple definitions.

     

    Wednesday, September 13, 2006 7:14 PM
    Moderator
  • Within the StrToArr namespace, I tried:

    protected: inline void StrToCharArray(String^ str);

    I still received the same errors...

    Wednesday, September 13, 2006 7:41 PM
  • That's not a code definition.  That's a prototype.  inline has no effect here.

    Let me take a step back: you are multiple definitions of data not functions.  Where are you actually defining  "struct Record theRecord"?  If it is in a header file, you need to appoint a cpp file where the definition is to occur.  If you need access from other cpp files, then use extern struct Record theRecord in the *.h file (which effectively prototypes it).

    Brian

     

    Wednesday, September 13, 2006 9:21 PM
    Moderator
  • I've defined "struct Record theRecord" directly under the #pragma once of the Form1.h file.
    Thursday, September 14, 2006 1:51 PM
  • This is where the definitions are in my Form1.h file:

    #include "stdafx.h"

    #include <stdio.h>

    #pragma once

    struct Record {

    char arr[15];

    };

    Record theRecord;

    errno_t err_temp;

    FILE *tempfile, *result;

    namespace StrToCharArr {

    using namespace System;

    using namespace System::ComponentModel;

    using namespace System::Collections;

    using namespace System::Windows::Forms;

    using namespace System::Data;

    using namespace System::Drawing;

    ...

    Thursday, September 14, 2006 1:57 PM
  • So my last post should address your problem.  Let me know if it doesn't.
    Thursday, September 14, 2006 1:57 PM
    Moderator
  • Using extern still would not work for me. I don't know what I'm doing wrong.
    Thursday, September 14, 2006 1:59 PM
  • Record theRecord defines theRecord on every cpp file that includes form.h.  This causes the linker to see more than one definition of this variable and you get an error..

    Move this definition to the actual cpp file where you need to use it.  If more than one cpp file needs it then add a new line extern Record theRecord in the common header file. 

    Also as time progresses, try to learn to code without declaring global variables as you are doing here.  Things like this really belong as members of a class.

    Thursday, September 14, 2006 2:21 PM
    Moderator