0

I am trying to learn C++ in VS 2010 Express. I want to transfer text from one textbox on a form to another when the Execute button is pressed. However, I want to learn how to do this in a different cpp file.

In the header file Form1.h, I have the code:

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
         {
             this-> textBox2-> Text = returnString(this-> textBox1 -> Text);

with #include "String_Copy.cpp" at the top.

In String_Copy.cpp, I have:

#include "stdafx.h"
using System::String;

String^ returnString(String^ input)
{
return input;
}

But I get the compile error:

String_Copy.obj : error LNK2005: "class System::String ^ __clrcall returnString(class System::String ^)" (?returnString@@$$FYMP$AAVString@System@@P$AAV12@@Z) already defined in Forms_Call.obj

I have no idea what I am doing wrong. Any suggestions?

Thanks.

  • `.h` is the header. `.cpp` is the source file. Generally speaking, you `#include` header files inside source files, not the other way around. – Dark Falcon Aug 13 '12 at 17:01
  • Visual Studio puts all of the code in the header file. Is there a way to move that code into a cpp file instead? I know that this is a rookie subject. Do you know of a tutorial for the convention of how this would typically be laid out in Visual C++? Thanks again. – user1595987 Aug 13 '12 at 17:37
  • 1
    You are not learning C++, you are actually using C++/CLI, a very different language. Which otherwise has the same rule as C++, a function definition can only appear once. Every .cpp file that #includes your .h file will generate yet another copy of the function. – Hans Passant Aug 13 '12 at 18:03

0 Answers0