I'm trying to make a windows form application in Visual Studio C++, but I get this errors after compiling, for each function:
error LNK2005: function already defined in MyForm.obj
These are my files:
Source.cpp
#pragma once
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]//leave this as is
void main() {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Project1::MyForm);
}
MyForm.h
#pragma once
#include <iostream>
#include <string>
#include "Header.h"
namespace Project1 {
//codes of te form
}
Header.h
#pragma once
#include <iostream>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int n, m;
int size1, size2;
//My functions here
So how can I fix errors?
If you declare and implement a function in a header file (Header.h) and if this file gets included twice, then you'll most likely will get a function already defined error at some point.
This can be fixed by either:
Moving function implementation to a source (cpp) file and only keep it's declaration in the header (h) file
Make the function inline (if it is acceptable), that will remove the error
Related
I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/
I want to use ChatScript externally in my program. In the documents it says:
Embedding Step #1 First, you will need to modify `common.h and compile the system. You need to add all the CS .cpp files to your build list.
Find the // #define NOMAIN 1 and uncomment it. This will allow you to compile your program as the main program and ChatScript merely as a collection of routines to accompany it.
But I am newbie in Linux and can’t understand how to add .cpp files to my build list? What is my build list? May someone explains what do should I do exactly?
I did copy all the .cpp and .h and other folders existed inside ChatScript/SRC directory beside my main.cpp in my project.
Then I tried to run this code:
#include<iostream>
using namespace std;
char* output2;
unsigned int InitSystem(int argc,char* argv[],char* unchangedPath,char* readonlyPath,char* writablePath);
void InitStandalone();
void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
int main()
{
PerformChat(NULL,NULL,"hi",NULL,output2);
cout<<output2;
return 0;
}
But I get this error message:
undefined reference to `PerformChat(char*, char*, char*, char*, char*)
Then I did include all the header files to my program and delete this line of code: void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
#include<iostream>
#include "common.h"
#include "common1.h"
#include "constructCode.h"
#include "cs_ev.h"
#include "csocket.h"
#include "dictionaryMore.h"
#include "dictionarySystem.h"
#include "english.h"
#include "evserver.h"
#include "factSystem.h"
#include "functionExecute.h"
#include "infer.h"
#include "jsmn.h"
#include "json.h"
#include "mainSystem.h"
#include "markSystem.h"
#include "mongodb.h"
#include "mprintf.h"
#include "multi.h"
#include "my_sql.h"
#include "os.h"
#include "outputSystem.h"
#include "patternSystem.h"
#include "postgres.h"
#include "privatesrc.h"
#include "scriptCompile.h"
#include "spellcheck.h"
#include "systemVariables.h"
#include "tagger.h"
#include "testing.h"
#include "textUtilities.h"
#include "tokenSystem.h"
#include "topicSystem.h"
#include "userCache.h"
#include "userSystem.h"
#include "variableSystem.h"
using namespace std;
char* output2;
unsigned int InitSystem(int argc,char* argv[],char* unchangedPath,char* readonlyPath,char* writablePath);
void InitStandalone();
void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
int main()
{
PerformChat(NULL,NULL,"hi",NULL,output2);
cout<<output2;
return 0;
}
But the new error says:
error: conflicting declaration of C function ‘int main()'
You would have to include all the chatscript SRC files in your project to get the function PerformChat to compile. But shortly ChatScript will release with library compilations as well.
I am using visual studio professional 2013. I have attached my header file with function prototypes to my main file. The compiler is saying that "'assignNum': identifier not found." I have absolutely no clue what's wrong, but I figured somebody else might. Here's the relevant code:
Main File
#include "Bullets.h"
#include "stdafx.h"
#include <ctime>`enter code here`
#include <cstdlib>
int main()
{
srand(time(NULL));
greetings();
assignNum();
return 0;
}
Header File
#include "stdafx.h"
using namespace std;
void greetings();
void assignNum();
If you are using the precompiled header feature...
#include "stdafx.h"
Must be the first line in your cpp file. Remove it from your h file.
I'm currently using the triangle library in my program. The library contains only .c and .h files (no .lib). I get the following error on Visual Studio C++ 2010:
1>data.obj : error LNK2019: unresolved external symbol _triangulate referenced in function "struct triangulateio __cdecl readfile(void)" (?readfile##YA?AUtriangulateio##XZ)
The header file of my data.cpp is the following:
#ifndef DATA_H
#define DATA_H
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <time.h>
#include <GL/gl.h> //include the gl header file
#include <GL/glut.h> //include the glut header file
#include <GL/glu.h> //include the glut header file
#include <armadillo>
//Namespace
using namespace std;
using namespace arma;
extern "C"
{
#ifdef SINGLE
#define REAL float
#else /* not SINGLE */
#define REAL double
#endif /* not SINGLE */
#include "triangle.h"
}
triangulateio readfile();
#endif
Data.cpp
triangulate("pczAevn", &in, &mid, &vorout);
I've already made my program work with a Makefile of mine on Ubuntu, but I need to run my program on windows.
Feel free to ask for more information.
EDIT #1:
If you use the triangle library with VS, you have to put the following instruction on top of the triangle.c file #define TRILIBRARY
Now it compile. Thank you very much for the help.
The linker can't find a definition for "triangulateio readfile()", if it's defined in the .c file my guess is that it isn't built. If you include it in the project it could work.
//head.h//
extern int sum(int,int);
//head.cpp//
#include "head.h"
#include "stdafx.h"
int sum(int x, int y)
{
return (x+y);
}
//mainfn.cpp//
#include "head.h"
#include "stdafx.h"
#include string
#include iostream
#include stdio.h
using std::string;
using std::cout;
using namespace System;
int main()
{
int x=10,y=2;
printf("value: %d",sum(x,y));
Console::ReadLine();
return 0;
}
While buliding in Visual studio 2005, this vc++ project is giving following error:
error C3861: 'sum': identifier not found.
Can anybody help me out with this?
You need to place the inclusion of head.h after stdafx.h. When precompiled headers are enabled the compiler will ignore the contents of all includes that occur prior to (in this case) the inclusion of stdafx.h .
Either remove stdafx.h from the project, and turn of precompiled headers.. or try moving head.h to be included after stdafx.h instead of before.