Can't access function from header file - c++

//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.

Related

#include "stdafx.h" Error for C++ Tutorial [duplicate]

When I compiled this program (from C++ Programming Language 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h only for the _TCHAR macro.
Then, precompiled header is usually a per-project file in Visual Studio world, so:
Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
Change the #include <stdafx.h> to #include "stdafx.h". It is supposed to be a project local file, not to be resolved in include directories.
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h.
Just include windows.h instead of stdfax or create a clean project without template.
There are two solutions for it.
Solution number one:
1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)
Solution Number two:
1.Create stdafx.h and stdafx.cpp in your project
2 Right click on project -> properties -> C/C++ -> Precompiled Headers
3.select precompiled header to create(/Yc)
4.Rebuild the solution
Drop me a message if you encounter any issue.
You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.
Add #include "afxwin.h" in your source file. It will solve your issue.
Just running through a Visual Studio Code tutorial and came across a similiar issue.
Replace #include "stdafx.h" with #include "pch.h" which is the updated name for the precompiled headers.

Error LNK2005: function already defined in MyForm.obj

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, Defined Function Not Recognized?

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.

Error C1083: Cannot open include file: 'stdafx.h'

When I compiled this program (from C++ Programming Language 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h only for the _TCHAR macro.
Then, precompiled header is usually a per-project file in Visual Studio world, so:
Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
Change the #include <stdafx.h> to #include "stdafx.h". It is supposed to be a project local file, not to be resolved in include directories.
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h.
Just include windows.h instead of stdfax or create a clean project without template.
There are two solutions for it.
Solution number one:
1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)
Solution Number two:
1.Create stdafx.h and stdafx.cpp in your project
2 Right click on project -> properties -> C/C++ -> Precompiled Headers
3.select precompiled header to create(/Yc)
4.Rebuild the solution
Drop me a message if you encounter any issue.
You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.
Add #include "afxwin.h" in your source file. It will solve your issue.
Just running through a Visual Studio Code tutorial and came across a similiar issue.
Replace #include "stdafx.h" with #include "pch.h" which is the updated name for the precompiled headers.

windows.h already included afxv_w32.h - CString

Am trying to include CString in my cpp file but am getting this error if i include afxinet.h
"windows.h already included afxv_w32.h"
These are my header files :
#include stdafx.h
#include shlwapi.h
#include Tlhelp32.h
#include hooks.h
#include stdio.h
#include common1.h
#include SafeLogger.h
#include io.h
#include tinyxml.h
#include winsock.h>
#pragma comment(lib, "Ws2_32.lib")
#include afxinet.h
I havent included the gaurds in stack to display here.
how to solve this issue and get to include CString in my file
Why are you adding afxinet.h if you want CString?
You should include atlstr.h to get CString - particularly if your project isn't MFC based.
Use /showIncludes option in C++ settings (Under Advanced). This will show how headers are being included (in tree-format). As another suggestion, you should include only files that are needed.
By including < afx.h >, you should be able to use CString class in cpp file.
#include <afx.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CString str("Santosh");
wcout << LPCTSTR(str) << endl;
return 0;
}
Additionally, you will need to define _AFXDLL preprocessor