Linking error LNK2005 with * __cdecl

I'm writing a program of array processing, but I've got a linking error and can't compile it. The message is
error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)

The program uses no classes exepting the Form1, I've removed all the others. But the error still presents. The asteric is used for multiplication, for array paramters and for files using by fopen etc. functions. The types are only int, double and bool, and there is a FILE handle. One additional unit provides only some functions and has only
1
2
#include "StdAfx.h"
#include <math.h> 

in impelemetation. I've tryed to comment various sections but the error doesn't disappear. Tell me please how can I cope with such linking error.
You're redefining a method that already exists in some static library. Post the code and I might be able to tell you what's causing it.
The first module Fom1.h code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma once
#include "afxdlgs.h"
#include <stdio.h>
#include <stdlib.h>
#include "SimAnneal.h"


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

	double dataArray [10000];
	/// <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 __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		int data_length;
		Form1(void)
		{
			InitializeComponent();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::Button *  button1;
	private: System::Windows::Forms::TextBox *  textBox1;
	private: System::Windows::Forms::Button *  button2;

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

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = new System::Windows::Forms::Button();
			this->textBox1 = new System::Windows::Forms::TextBox();
			this->button2 = new System::Windows::Forms::Button();
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(16, 72);
			this->button1->Name = S"button1";
			this->button1->Size = System::Drawing::Size(152, 40);
			this->button1->TabIndex = 0;
			this->button1->Text = S"Load data...";
			this->button1->Click += new System::EventHandler(this, button1_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(16, 32);
			this->textBox1->Name = S"textBox1";
			this->textBox1->Size = System::Drawing::Size(168, 22);
			this->textBox1->TabIndex = 1;
			this->textBox1->Text = S"D:\\myinfomation\\simulated annealing\\h1.txt";
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(16, 128);
			this->button2->Name = S"button2";
			this->button2->Size = System::Drawing::Size(144, 40);
			this->button2->TabIndex = 2;
			this->button2->Text = S"Save results";
			this->button2->Click += new System::EventHandler(this, button2_Click);
			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(6, 15);
			this->ClientSize = System::Drawing::Size(292, 268);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button1);
			this->Name = S"Form1";
			this->Text = S"Form1";
			this->ResumeLayout(false);

		}	
	private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 	/*CFileDialog fdlg (TRUE);
					if(fdlg.DoModal()!=IDOK)
						return;
					CString fname = fdlg.GetPathName();*/
				 FILE *fid = fopen(/*fname*/ /*textBox1 -> Text ->ToCharArray ()*/"D:\\myinfomation\\simulated annealing\\h1.txt", "r");
					int i = 0;
					while((!feof(fid)) && (i<10000)) {
						fscanf(fid, "%f", &dataArray [i]);
						i++;
					}
					data_length = i;
					fclose(fid);
//	return TRUE;
			 }

	private: System::Void button2_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 int sa_res [1000];
				 int res_length = SeparateChart (dataArray, sa_res, data_length, 50, 6);
				 FILE *fid = fopen("C:\\temp\\sa_res.txt", "w");
				 for (int i = 0; i < res_length; i++)
					 fprintf (fid, "%f\r\n", &sa_res [i]);
				 fclose (fid);
			 }

	};
}


The additional module SimAnneal.h header is:

1
2
3
4
5
6

	int ApplySimAnneal(double *chart, int chart_length, int dir, int start, int scatter, int minx);
	int HighestCoincidence(int *values, int count);
	int RepeatSA(double * chart, int chart_length, int dir, int start, int scatter, int minx);
	int SeparateChart(double * chart, int * result, int chart_length, int nvs, int scatter);


And the module itself is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "StdAfx.h"
#include <cmath>
#include ".\simanneal.h"

int ApplySimAnneal(double *chart, int chart_length, int dir, int start, int scatter, int minx)
{
	double Tinit = 1;
	double minT = 1e-8;
	int max_consec_rejections = 1000;
	int max_try = 300;
	int max_success = 20;
	double k = 1;
	double Tgrad = 0.8;

	int itry = 0;
	int parent = start;
	int success = 0;
	bool finished = false;
	int consec = 0;
	double T = Tinit;
	double initenergy = chart [parent];
	double newenergy, oldenergy;
	int total = 0;
	int current, newparam=0;
	oldenergy = chart [0];
	while (!finished) {
		itry++;
		current=parent;
		if ((itry >= max_try) || (success >= max_success) || (T < minT) || (consec >= max_consec_rejections)) {
			finished = true;
			total = total + itry;
			break;
		}
		else {
			T = Tgrad*T;
			total = total + itry;
			itry=1;
			success = 1;
		}
		newparam = current + scatter*rand ();
		if (newparam < minx)
			newparam = minx;
		if (newparam >= chart_length)
			newparam = chart_length;
		newenergy = chart [newparam];
		if ((oldenergy - newenergy)*dir > 1e-6*dir) {
			parent = newparam;
			oldenergy = newenergy;
			success++;
			consec = 0;
		}
		else {
			if (rand ()*dir < dir*exp ((oldenergy - newenergy) / (k*T))){
				parent = newparam;
				oldenergy = newenergy;
				success++;
			}
			else
				consec++;
		}
	}
	return parent;
}

int HighestCoincidence(int *values, int count)
{
	int coinccount [10];
	int coincvalues [10];
	int coinctotal = 0;
	int i;
	for (i = 0; i < count; i++){
		bool coincfound = false;
		for (int j = 0; j < coinctotal; j++)
			if (values [i] == coincvalues [j]){
				coinccount [j]++;
				coincfound = true;
			}
			if (!coincfound) {
				coincvalues [coinctotal] = values [i];
				coinctotal ++;
			}
	}
	int maxcoinc = 0;
	for (i = 1; i < coinctotal; i++)
		if (coinccount [i] > coinccount [maxcoinc])
			i = maxcoinc;
	return coincvalues [maxcoinc];
}

int RepeatSA(double * chart, int chart_length, int dir, int start, int scatter, int minx)
{
	int sa_results [9];
	for (int i = 0; i < 9; i++)
		sa_results [i] = ApplySimAnneal (chart, chart_length, dir, start, scatter, minx);
	return HighestCoincidence (sa_results, 9);
}

int SeparateChart(double * chart, int * result, int chart_length, int nvs, int scatter)
{
	int dir, current = 0;
	int total = 0;
	if (RepeatSA (chart, chart_length, 1, nvs, scatter, 0) > RepeatSA (chart, chart_length, -1, nvs, scatter, 0))
		dir = -1;
	else
		dir = 1;
	while (current + nvs < chart_length){
		current = RepeatSA(chart, chart_length, dir, current + nvs, scatter, current);
		dir = -dir;
		result [total] = current;
		total++;
	}	
	return total;
}


As I could guess about possbile redefinings may be it can be #include "StdAfx.h" and #include "afxdlgs.h". But both modules don't work without it. So how to define it to make the project linkable.
What compiler are you using? What are your compiler/linker flags?
Topic archived. No new replies allowed.