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.
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/
This question already has answers here:
Why stdfax.h should be the first include on MFC applications? [duplicate]
(3 answers)
Closed 5 years ago.
I expect this is incredibly simple but can't for the life of me figure out whats wrong. I'm new to C++ and I've created a C++ class in visual studio and am trying to use it in the main method of another file. I've stripped everything back to the bare minimum but still I can't get it to run. The first compile error I get is 'Test': undeclared identifier. If I remove 'Test test;' from App.cpp it all compiles fine.
Below is the code. Can anybody help?
App.cpp:
#include "Test.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
Test test;
//cout << test.getNumber() << endl;
return 0;
}
Test.h:
#pragma once
class Test
{
private:
int number;
public:
int getNumber();
Test();
~Test();
};
Test.cpp:
#include "stdafx.h"
#include "Test.h"
int Test::getNumber()
{
return number;
}
Test::Test()
{
this->number = 1;
}
Test::~Test()
{
}
The problem is that "stdafx.h" is pre-compiled.
Visual c++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The solution is to move it to be the first include.
You can read more about it in the wiki page of Precompiled header.
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
Visual Studio is not recognising my #include 'Header.h' file. I have created the file in the Header Files in solution explorer and also tried manually pointing to the file. What I don't understand is, until yesterday there was absolutely no problem. Therefore, a simple cout doesn't work.
#include 'Header.h';
int main()
{
cout << "hi";
return 0;
}
#include "Header.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hi";
return 0;
}
Not that Header.h is used in anyway, this is still the correct syntax.
You need #include <iostream> to be able to use cout.
You've got syntax errors in your #include preprocessor directive. Replace single with double quotes and drop the semicolon:
#include "Header.h"
Found the problem : I had to go to properties bar and change "Included In Folder" value to true from false.
//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.