already defined function error

When using the form designer I get an error saying

"

------ Build started: Project: Sketch Pad, Configuration: Release Win32 ------

Compiling...

stdafx.cpp

C:\Program Files\Microsoft Visual Studio 8\VC\include\KamReg.h(20) : warning C4172: returning address of local variable or temporary

Compiling...

Sketch Pad.cpp

AssemblyInfo.cpp

Linking...

stdafx.obj : error LNK2005: "char * __clrcall ReadKey(char *,char *)" ( ReadKey@@$$FYMPADPAD0@Z) already defined in Sketch Pad.obj

C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Sketch Pad\Release\Sketch Pad.exe : fatal error LNK1169: one or more multiply defined symbols found

Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Sketch Pad\Sketch Pad\Release\BuildLog.htm"

Sketch Pad - 2 error(s), 1 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

"

Here is the code for my header

"

#include "windows.h"

#include <winreg.h>

#include <stdio.h>

#include "string"

char* ReadKey(char* path, char* key)

{

HKEY hk;

char rgValue [1024];

DWORD size;

DWORD Type;

if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,(LPCWSTR) path,0,KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS)

{

size=1023;

RegQueryValueEx( hk,(LPCWSTR) key, NULL, &Type,(LPBYTE)rgValue,&size);

}

RegCloseKey(hk);

return rgValue;

}

void WriteKey(char* key, char* name, char* value, int size)

{

HKEY hk;

if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,(LPCWSTR)key,0,KEY_ALL_ACCESS,&hk))

{

exit(1);

}

if (RegSetValueEx(hk, // subkey handle

(LPCWSTR) name, // value name

0, // must be zero

REG_EXPAND_SZ, // value type

(LPBYTE) value, // pointer to value data

size)) // data size

{

exit(1);

}

RegCloseKey(hk);

}

void CreateKey(char* key)

{

HKEY hk;

DWORD dwHappened;

if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCWSTR) key,

0, NULL, REG_OPTION_NON_VOLATILE,

KEY_WRITE, NULL, &hk, &dwHappened))

{

exit(1);

}

RegCloseKey(hk);

}

"

And here is my Form1 code

"

#pragma once

namespace SketchPad {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

/// <summary>

/// Summary for Form1

///

/// 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 Form1 : public System::Windows::Forms::Form

{

public:

char* close;

Form1(void)

{

InitializeComponent();

}

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::RichTextBox^ richTextBox1;

private: System::Windows::Forms::Label^ label1;

private: System::Windows::Forms::Timer^ timer1;

private: System::ComponentModel::IContainer^ components;

protected:

private:

/// <summary>

/// Required designer variable.

/// </summary>

#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->components = (gcnew System::ComponentModel::Container());

System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));

this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());

this->label1 = (gcnew System::Windows::Forms::Label());

this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));

this->SuspendLayout();

//

// richTextBox1

//

this->richTextBox1->BackColor = System::Drawing::Color::Yellow;

this->richTextBox1->Location = System::Drawing::Point(12, 77);

this->richTextBox1->Name = L"richTextBox1";

this->richTextBox1->Size = System::Drawing::Size(295, 234);

this->richTextBox1->TabIndex = 0;

this->richTextBox1->Text = L"";

//

// label1

//

this->label1->AutoSize = true;

this->label1->BackColor = System::Drawing::Color::Yellow;

this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

static_cast<System::Byte>(0)));

this->label1->Location = System::Drawing::Point(99, 49);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(122, 25);

this->label1->TabIndex = 1;

this->label1->Text = L"Sketch Pad";

//

// timer1

//

this->timer1->Enabled = true;

this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"$this.BackgroundImage")));

this->ClientSize = System::Drawing::Size(329, 358);

this->Controls->Add(this->label1);

this->Controls->Add(this->richTextBox1);

this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;

this->Name = L"Form1";

this->Text = L"Form1";

this->TransparencyKey = System::Drawing::Color::Lime;

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

public:

void Loop(void)

{

/// <summary>

/// Main Loop here.

/// </summary>

close = ReadKey("Software\\Interactive Desktop", "Close");

}

public: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {

Loop();

}

};

}

"

I am reletivly new to C++ so could someone tell me what is going wrong



Answer this question

already defined function error

  • LawrenceSQL

    A function can have only one definition. When you place the code in the header, the same functions definition is included into more than one C++ file.

    Place only the declaration
    char* ReadKey(char* path, char* key);
    into a header file. Place the complete function into a seperate CPP file.



  • Tallier

    I must have done something wrong but I don't know what. I changed the header to

    "

    char* ReadKey(char* path, char* key);

    void WriteKey(char* key, char* name, char* value, int size);

    void CreateKey(char* key);

    "

    I saved my old header as a .cc file and clicked add existing item to my progect but that didn't work. Sorry, I am still new to C++.


  • already defined function error