header file include cpp file in visual studio gives compile error - c++

I have my header like:
#include iostream & map
namespace dummy {
const char* some_xxx_name(const char* name);
}
#include myfile.cpp (the following cpp file stated below)
I have my cpp file like:
#include "myfile.h" (the file stated above)
#include <sstream>
#include <cassert>
inline const char* dummy::some_xxx_name(const char* name) {
........
}
They are working fine under my linux environment with makefile, now I am adopting in visual studio environment and get following error:
error C2084: function 'const char *dummy::some_xxx_name(const char *)' already has a body ....(point to the line in the header)
I know the file structure is not good at all for including cpp at header and including header at cpp(or maybe).
want to ask if there is any chance to make it work without updating the code? Not familiary how shall I go forward under windows visual studio environment.

Related

Not able to include standard c++ library headers in vs code esp-idf extension

I am not able to include some standard libraries:
#include <stdio.h> // <= this works
// #include <thread> // <= error: "No such file or directory"
// #include <algorithm> // <= error: "No such file or directory"
void app_main(void)
{
}
The error is generated by compiler xtensa-esp32-elf-gcc.exe. There is no error from C/C++ Intellisense. And I am able to find the necessary header by pressing F12. They are placed on the path C:\Espressif\tools\xtensa-esp32-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\8.4.0. There is no such path in the compiler -I arguments.
How can I deal with this error?
Thread and algorithm are both part of C++ standard library, not C standard library. I've renamed the source file to .cpp (CMakeLists.txt was updated by VSCode automatically). Then I added:
extern "C" {
void app_main();
}
And it works. Hope it helps somebody.

After defining a function in another file, visual studio does not recognize the function

I'm creating windows console app that has many pages (split in files). I'm facing a problem when executing the program and Visual Studio throws 'startpage' identifier not found error (startpage is the function and the file name is startpage.h)
I've tried using:
external int startpage(); and
int startpage();.
I've tried changing only the function name too (so the file and function don't use the same name).
I have no idea why this is happening. Other files with different functions are working. The file "startpage.h" uses two functions defined in other files, and those are not throwing any errors.
#include "include/startpage.h"
#include <iostream>
#include <conio.h>
#include "include/concol.h"
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
startpage();
}
```
Here is the error:
Error code: C3861: 'startpage' identifier not found
Move #include "pch.h" to the top. The compiler ignores everything above the inclusion of precompiled header. – Igor Tandetnik
This worked!
Thank you so much guys!

Error in creating a class header file

I am attempting to create a simple class header file. I have done the following so far:
#ifndef RECORD_H
#define RECORD_H
class Records{
int idNumber;
int serialNumber;
public:
Records();
};
#endif
However, I am getting the following error:
[Error] unknown type name 'class'
What am I doing wrong?
You're probably compiling it as C rather than C++. That is why you are getting that error..
Make sure your source file has a .cpp extension.
As lakesh said, you may to compiling as C rather than C++. Most compiler use the file extension to determine how to compile the file (use .cpp instead of .c)
If that's not it, then the problem would probably be in the a different file loaaded before this.
// Record.cpp
#include "badfile.h" // error in here
#include "record.h" // error showing up here.

Is it possible to force visual studios to throw an error if an identifiers is used when its library isn't directly included in the source?

When compiling the following source:
int main()
{
exp(1.0);
return 0;
}
the copiler gives the following error: error C3861: 'exp': identifier not found because I didn't have the line: #include <iostream> above main()
However, visual studios won't display the error if a library is indirectly included. For example, the following code compiles without a problem even though the dependency of exp is in <cmath>.
#include <istream>
int main()
{
exp(1.0);
return 0;
}
This is because <iostream> includes <istream> which include <ostream> which includes <ios> which includes <xlocnum> which includes <cmath>.
Is there a way to make visual studios throw an error if I don't explicitly include a library yet try to use one of its identifiers?
You may want to have a look at include-what-you-use. It is a clang-based tool trying to detect missing and superfluous include directives.

C++ Class Static Members

So I have my header file with static members:
#ifndef PROFILE_MANAGER_H
#define PROFILE_MANAGER_H
#include <map>
#include <vector>
using namespace std;
using std::vector;
namespace Engine
{
class ProfileManager
{
private:
static map<const char*, vector<float>> profiles;
static map<const char*, vector<float>>::iterator it;
static pair<map<const char*, vector<float>>::iterator, bool> ret;
};
}
#endif
And in my cpp file i have the definitions:
#include "ProfileManager.h"
namespace Engine
{
map<const char*, vector<float>> ProfileManager::profiles;
map<const char*, vector<float>>::iterator ProfileManager::it;
pair<map<const char*, vector<float>>::iterator, bool> ProfileManager::ret;
}
The linker always complains about the static members being unresolved externals (LNK2001) even though I have defined them in the cpp file. Any ideas as to why?
These kind of errors usually happen when the linker is not given the obj file that is the result of the compilation of cpp.
Look for ProfileManager.obj in your output directory. If it doesn't exist, there is something wrong. Possibly the cpp file is not compiled as Luchian Grigore suggested. It's also possible that the linker is not given the obj file in the parameters. If you are using visual studio, check that the cpp file is a part of the project. In other environments see the command the linker is invoked with.
If you use Visual Studio, you can open the project properties -> Linker -> Command Line and add /VERBOSE in Additional Option. Then open you output window and recompile the project. (Thank you, Craig for the comment).
One more scenario that could have happened. You included the header file in another project and you tried to build without referencing the project where ProfileManager.cpp was.