【问题标题】:redefinition; multiple initialization重新定义;多重初始化
【发布时间】:2012-09-14 02:16:04
【问题描述】:

我正在使用 Visual Studio 2005 中的 C++/CLI 编写一个包含辅助表单的程序。到目前为止,由于一对没有多大意义的重新定义错误,我没有取得太大进展。

Mode.h(12) : error C2374: 'NameManipulator::check' : redefinition; multiple initialization
Mode.h(12) : see declaration of 'NameManipulator::check'
Mode.h(22) : error C2011: 'NameManipulator::Mode' : 'class' type redefinition
Mode.h(22) : see declaration of 'NameManipulator::Mode'

我只在一个命名空间中声明了这些中的每一个。一个甚至是由编译器预先生成的。除了从头开始之外,我还能做些什么来解决这个问题?任何帮助将非常感激。 (代码如下)

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

namespace NameManipulator {
int check = 4;
/// <summary>
/// Summary for Mode
///
/// WARNING: If you change the name of this class, you will need to change the
///          'Resource File Name' property for the managed resource compiler tool
///          associated with all .resx files this class depends on.  Otherwise,
///          the designers will not be able to interact properly with localized
///          resources associated with this form.
/// </summary>
public ref class Mode : public System::Windows::Forms::Form
{
public:
    Mode(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Mode()
    {
        if (components)
        {
            delete components;
        }
    }
public: System::Windows::Forms::RadioButton^  rdoAllCaps;
public: System::Windows::Forms::RadioButton^  rdoAllLow;
public: System::Windows::Forms::RadioButton^  rdoReverse;
public: System::Windows::Forms::RadioButton^  rdoNormal;
private: System::Windows::Forms::Button^  btnOK;
protected: 

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->rdoAllCaps = (gcnew System::Windows::Forms::RadioButton());
        this->rdoAllLow = (gcnew System::Windows::Forms::RadioButton());
        this->rdoReverse = (gcnew System::Windows::Forms::RadioButton());
        this->rdoNormal = (gcnew System::Windows::Forms::RadioButton());
        this->btnOK = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // rdoAllCaps
        // 
        this->rdoAllCaps->AutoSize = true;
        this->rdoAllCaps->Location = System::Drawing::Point(12, 12);
        this->rdoAllCaps->Name = L"rdoAllCaps";
        this->rdoAllCaps->Size = System::Drawing::Size(75, 17);
        this->rdoAllCaps->TabIndex = 0;
        this->rdoAllCaps->Text = L"ALL CAPS";
        this->rdoAllCaps->UseVisualStyleBackColor = true;
        // 
        // rdoAllLow
        // 
        this->rdoAllLow->AutoSize = true;
        this->rdoAllLow->Location = System::Drawing::Point(12, 35);
        this->rdoAllLow->Name = L"rdoAllLow";
        this->rdoAllLow->Size = System::Drawing::Size(63, 17);
        this->rdoAllLow->TabIndex = 1;
        this->rdoAllLow->Text = L"all lower";
        this->rdoAllLow->UseVisualStyleBackColor = true;
        this->rdoAllLow->CheckedChanged += gcnew System::EventHandler(this, &Mode::rdoAllLow_CheckedChanged);
        // 
        // rdoReverse
        // 
        this->rdoReverse->AutoSize = true;
        this->rdoReverse->Location = System::Drawing::Point(12, 58);
        this->rdoReverse->Name = L"rdoReverse";
        this->rdoReverse->Size = System::Drawing::Size(71, 17);
        this->rdoReverse->TabIndex = 2;
        this->rdoReverse->Text = L"rEVERSE";
        this->rdoReverse->UseVisualStyleBackColor = true;
        this->rdoReverse->CheckedChanged += gcnew System::EventHandler(this, &Mode::rdoReverse_CheckedChanged);
        // 
        // rdoNormal
        // 
        this->rdoNormal->AutoSize = true;
        this->rdoNormal->Checked = true;
        this->rdoNormal->Location = System::Drawing::Point(12, 81);
        this->rdoNormal->Name = L"rdoNormal";
        this->rdoNormal->Size = System::Drawing::Size(73, 17);
        this->rdoNormal->TabIndex = 3;
        this->rdoNormal->TabStop = true;
        this->rdoNormal->Text = L"rdoNormal";
        this->rdoNormal->UseVisualStyleBackColor = true;
        // 
        // btnOK
        // 
        this->btnOK->DialogResult = System::Windows::Forms::DialogResult::OK;
        this->btnOK->Location = System::Drawing::Point(32, 106);
        this->btnOK->Name = L"btnOK";
        this->btnOK->Size = System::Drawing::Size(33, 23);
        this->btnOK->TabIndex = 4;
        this->btnOK->Text = L"OK";
        this->btnOK->UseVisualStyleBackColor = true;
        // 
        // Mode
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(99, 138);
        this->ControlBox = false;
        this->Controls->Add(this->btnOK);
        this->Controls->Add(this->rdoNormal);
        this->Controls->Add(this->rdoReverse);
        this->Controls->Add(this->rdoAllLow);
        this->Controls->Add(this->rdoAllCaps);
        this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
        this->Name = L"Mode";
        this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
        this->Text = L"Mode";
        this->Load += gcnew System::EventHandler(this, &Mode::Mode_Load);
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion
private: System::Void rdoReverse_CheckedChanged(System::Object^  sender, System::EventArgs^  e){
             if (rdoReverse->Checked == true)
                 check = 3;
         }
private: System::Void Mode_Load(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void rdoAllLow_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
         }
};
}

【问题讨论】:

  • #pragma once 是否位于Mode.h 的顶部?看起来您在源文件中多次包含该文件。这很可能是因为您包含了包含Mode.h 的其他标头。如果您没有该编译指示,则可能会多次包含该文件,这将导致出现您所看到的错误消息。
  • 另外,请注意在标题中声明和初始化check。这将在包含标头的每个源文件中制作该变量的本地副本(而不是具有跨源共享的单个变量)。这可能不是您期望的行为。相反,您可能希望将其声明为 extern 并在 Mode.cpp 中定义它。
  • 我在 Mode 中包含了 Main(其中包含 Mode)。哎呀。修复后,它给了我这个:NameManipulator.obj : error LNK2005: "int NameManipulator::check" (?check@NameManipulator@@$$Q3HA) already defined in Mode.obj
  • 每个头文件的顶部是否有#pragma once
  • 是的,我愿意。并且使用extern 似乎在某种程度上使情况变得更糟,导致unresolved token 错误。

标签: visual-c++ visual-studio-2005 c++-cli visual-c++-2005


【解决方案1】:

你的错误是你定义头文件中的check变量:

namespace NameManipulator {
    int check = 4;
    // Error in the line above

将其更改为声明,如下所示:

namespace NameManipulator {
    extern int check;

并在源文件中添加定义:

int NameManipulator::check = 4;

【讨论】:

    【解决方案2】:

    当您使用多个模块(.h 和 .cpp 文件)时,重新定义错误可能会发生在简单的事情上,例如拼写错误。我犯的错误如下:

    #ifndef NAMESPACE_FILENAME_H
    #define NAMESPACE_FILEMANE_H
    #include <iostream>
    #include "helperFile.h"
    
    
    /* you code and stuff here  */
    
    #endif
    

    我们使用 #ifndef#endif,因此编译器可以忽略不同文件上的多个声明,并且只有 1 个定义实例。现在,如果你回到我声明的第二行。我做了一个错字 fileMane 而不是文件名。编译器会以你的方式经历很多错误,除了这个。所以是的,请注意拼写错误!

    PS:课程费用:用细齿梳检查我的代码需要 5 小时。

    【讨论】:

      【解决方案3】:

      如果多个项目源文件具有同名的全局变量,将 Enable Unity (JUMBO) Build 设置为 YES 也会导致发生此错误。

      【讨论】:

        猜你喜欢
        • 2019-04-28
        • 1970-01-01
        • 2021-07-26
        • 2021-06-03
        • 2010-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多