C++ in Visual Studio 2013 - <Class> is undefined - c++

I'm very new to C++/VS and might missing something in the code/configuration of my project.
In my solution I have 2 projects:
first is NTL which I downloaded from https://bitbucket.org/ben_key/ntl, and compiled to a static library NTL.lib.
a 'test' project in which: (1) I added the header files by specifying their directory in the properties->C++->Additional Include Files, (2) in the properties->Linker->Input->Additional Dependencies I added "NTL.lib" (3)copied the NTL.lib file to be in the same directory as the main cpp file of the 'test' project.
My cpp only contains:
#include <NTL/GF2X.h>
int main() {
GF2X P;
return 1;
}
The build gives the output:
1>------ Build started: Project: test, Configuration: Release Win32 ------
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets(388,5): warning MSB8028: The intermediate directory (Release\) contains files shared from another project (ntl-test.vcxproj). This can lead to incorrect clean and rebuild behavior.
1> QuickTest.cpp
1>..\tests\QuickTest.cpp(43): warning C4101: 'n' : unreferenced local variable
1>D:\studies\Thesis\NTL-Ben-Key\Include\NTL/vector.h(79): warning C4291: 'void *operator new(size_t,_ntl_vector_placement)' : no matching operator delete found; memory will not be freed if initialization throws an exception
1> D:\studies\Thesis\NTL-Ben-Key\Include\NTL/vector.h(36) : see declaration of 'operator new'
1> D:\studies\Thesis\NTL-Ben-Key\Include\NTL/vector.h(319) : see reference to function template instantiation 'void NTL::BlockConstruct<T>(T *,long)' being compiled
1> with
1> [
1> T=NTL::zz_p
1> ]
1> D:\studies\Thesis\NTL-Ben-Key\Include\NTL/vector.h(291) : while compiling class template member function 'void NTL::Vec<NTL::zz_p>::DoSetLength(long)'
1> D:\studies\Thesis\NTL-Ben-Key\Include\NTL/vector.h(115) : see reference to function template instantiation 'void NTL::Vec<NTL::zz_p>::DoSetLength(long)' being compiled
1> D:\studies\Thesis\NTL-Ben-Key\Include\NTL/vec_lzz_p.h(14) : see reference to class template instantiation 'NTL::Vec<NTL::zz_p>' being compiled
1> MyTest.cpp
1>MyTest.cpp(4): error C2065: 'GF2X' : undeclared identifier
1>MyTest.cpp(4): error C2146: syntax error : missing ';' before identifier 'P'
1>MyTest.cpp(4): error C2065: 'P' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It is really a simple thing and I didn't figured out what I'm missing.

From NTL/include/NTL/ tools.h:
#define NTL_NAMESPACE NTL
#define NTL_OPEN_NNS namespace NTL_NAMESPACE {
#define NTL_CLOSE_NNS }
So when the preprocessor encounters NTL_OPEN_NNS, as is the case in the include file GF2X.h, it expands it to namespace NTL meaning the GF2X class is declared insided the namespace NTL. In order to use it you need to fully qualify it as NTL::GF2X or use using namespace NTL for discussion about which one look here for example.
Likewise at the end of GF2X.h there is a closing bracket after expanding NTL_CLOSE_NNS

Related

Using an external class in templated method from dll

I'm trying to use my game engine into an external project. Everything works fine but I have a problem when I'm trying to create a custom component (That inherits from ABhaviour which is an abstract class representing a component that can be activated/disabled).
GameObject.h (exported with __declspec(dllexport))
// [...]
template <typename Class, typename ... Args>
std::shared_ptr<Class> GameObject::AddComponent(Args... args)
{
auto newComp = std::make_shared<Class>(std::forward<Args>(args)...);
m_components[typeid(*newComp).hash_code()] = newComp;
m_components[typeid(*newComp).hash_code()]->SetOwner(*this);
return newComp;
}
// [...]
In an other project (That use my game engine), I'm trying to create the custom ABehaviour :
PlayerController.h
#pragma once
#include <ElkGameEngine/Objects/Components/Behaviours/ABehaviour.h>
class PlayerController :ElkGameEngine::Objects::Components::Behaviours::ABehaviour
{
PlayerController() = default;
~PlayerController() = default;
void Update();
};
After that I add my custom component onto my "Player" GameObject :
Main.cpp
#include <ElkGameEngine/ElkGameEngine.h>
#include <ElkGameEngine/Objects/AObject.h>
#include <ElkGameEngine/Objects/Components/AComponent.h>
#include "PlayerController.h"
using namespace ElkGameEngine::Objects::Components;
using namespace ElkGameEngine::Objects::Components::Behaviours;
int main()
{
ElkGameEngine::Managers::EngineManager elkGameEngine;
elkGameEngine.GetSceneManager()->CreateScene("ElkCraft");
auto& player = elkGameEngine.GetSceneManager()->CreateEmptyGameObject("Player");
player.AddComponent<PlayerController>(); /* Here is the problem */
while (elkGameEngine.IsRunning())
{
elkGameEngine.PreUpdate();
elkGameEngine.PostUpdate();
}
return EXIT_SUCCESS;
}
Visual Studio is unhappy and print me this :
Severity Code Description Project File Line Suppression State
Error C2440 '<function-style-cast>': cannot convert from 'const std::shared_ptr<PlayerController>' to 'std::shared_ptr<ElkGameEngine::Objects::Components::AComponent>' ElkCraft c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory 1472
Error C2228 left of '.swap' must have class/struct/union ElkCraft c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory 1472
Output window :
1>------ Build started: Project: ElkCraft, Configuration: Debug x64 ------
1>Could Not Find C:\Users\adrie\Desktop\GROUP_2\PFA\ElkCraft\*.dll
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(1472): error C2440: '<function-style-cast>': cannot convert from 'const std::shared_ptr<PlayerController>' to 'std::shared_ptr<ElkGameEngine::Objects::Components::AComponent>'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(1472): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\adrie\desktop\group_2\pfa\elkengine\build\elkgameengine\include\elkgameengine\objects\gameobject.h(53): note: see reference to function template instantiation 'std::shared_ptr<ElkGameEngine::Objects::Components::AComponent> &std::shared_ptr<ElkGameEngine::Objects::Components::AComponent>::operator =<PlayerController>(const std::shared_ptr<PlayerController> &) noexcept' being compiled
1>c:\users\adrie\desktop\group_2\pfa\elkengine\build\elkgameengine\include\elkgameengine\objects\gameobject.h(55): note: see reference to function template instantiation 'std::shared_ptr<ElkGameEngine::Objects::Components::AComponent> &std::shared_ptr<ElkGameEngine::Objects::Components::AComponent>::operator =<PlayerController>(const std::shared_ptr<PlayerController> &) noexcept' being compiled
1>c:\users\adrie\desktop\group_2\pfa\elkcraft\sources\main.cpp(18): note: see reference to function template instantiation 'std::shared_ptr<PlayerController> ElkGameEngine::Objects::GameObject::AddComponent<PlayerController,>(void)' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(1472): error C2228: left of '.swap' must have class/struct/union
1>Generating Code...
1>Compiling...
1>PlayerController.cpp
1>Generating Code...
1>Done building project "ElkCraft.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I think that the problem come from the fact that my game engine (Which is a group of DLL) is exporting a templated method (AddComponent and other ones like GetComponent).
Is it possible to use a templated method from a dll with a class/struct template argument from the project using the dll ?
Am I doing something wrong ?
Do I need to specify something on my PlayerController to make it "known/usable" in the AddComponent method ?

Error C2664 when trying to create unique_ptr

I'm trying to figure out a problem I have in my project, and I have simplified it down to this little bit of code that generates the C2664 error. I don't understand the error message, could anyone help me to understand? I've googled, and I've looked through 2 C++ books and this code is exactly what is listed in them, but it does not work for me.
Thanks.
#include <memory>
struct A
{
int b;
};
int main(int argc, char ** argv)
{
A a;
std::unique_ptr<A> a_ptr = std::make_unique<A>(new A);
return 0;
}
And here is the error:
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2585): error C2664: 'A::A(const A &)': cannot convert argument 1 from 'A *' to 'A &&'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2584): note: Reason: cannot convert from 'A *' to 'A'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2584): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\users\aksel\documents\visual studio 2017\projects\project1\project1\main.cpp(21): note: see reference to function template instantiation 'std::unique_ptr<A,std::default_delete<_Ty>> std::make_unique<A,A*,0>(A *&&)' being compiled
1> with
1> [
1> _Ty=A
1> ]
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The argument to make_unique is the argument to a T constructor, not a pointer to a T instance, just use the regular unique_ptr ctor if you already have a pointer to a T.

Adding std::thread to a preexisting project

I'm working off of a sample c++ that I was given, and I'm trying to add threading to the program.
If anywhere in the program I add the following:
#include <thread>
void someFunction () {
std::thread t([]() {std::cout << "Hello" << std::endl; });
}
It fails to compile with the error:
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thread(48): error C3083: 'std': the symbol to the left of a '::' must be a type
1> C:\Users\Dennis Jeong\Documents\Stanford\CS148\cs148-raytracer\common\RayTracer.cpp(20): note: see reference to function template instantiation 'std::thread::thread<RayTracer::Run::<lambda_d2fd5ec539e104442fc3c5987873ac7f>,,void>(_Fn &&)' being compiled
1> with
1> [
1> _Fn=RayTracer::Run::<lambda_d2fd5ec539e104442fc3c5987873ac7f>
1> ]
However, if I create a new c++ project in visual studio, this works.
Where could the error possibly be?
If it is helpful, the cmake file is here:
https://gist.github.com/w0nche0l/aef2a112b596f489705441f429e725b5
EDIT: additionally, I'm sure that it is not the lambda syntax that is causing this error, because I can create and run lambdas without threads fine.

Crypto++ giving a compiler error in algparam.h

I have the following lines in a rather large file:
#include <sha.h>
#include <hex.h>
Which, when compiled, throws this compiler error:
1>d:\work\app\tools\cryptopp\algparam.h(322): error C2061: syntax error : identifier 'buffer'
1> d:\work\app\tools\cryptopp\algparam.h(321) : while compiling class template member function 'void CryptoPP::AlgorithmParametersTemplate<T>::MoveInto(void *) const'
1> with
1> [
1> T=bool
1> ]
1> d:\work\app\tools\cryptopp\algparam.h(329) : see reference to class template instantiation 'CryptoPP::AlgorithmParametersTemplate<T>' being compiled
1> with
1> [
1> T=bool
1> ]
I'm pretty sure I'm forgetting something, but I'm not sure what. If I don't include hex.h, I don't have any problems and I get a SHA256 hash just fine, but when I do include hex.h, the error pops up.
Edit
In case anyone wonders, from algparam.h of Crypto++ toolkit:
void MoveInto(void *buffer) const //<=== line 320
{
AlgorithmParametersTemplate<T>* p = new(buffer)
AlgorithmParametersTemplate<T>(*this);
}
CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>; // <== line 329
Edit: Removed unrelated code
I fixed the problem by temporarily undefining new, which was defined as a macro to some extra debugging code.
#pragma push_macro("new")
#undef new
/* #includes for Crypto++ go here */
#pragma pop_macro("new")
If you're including Crypto++ in a Visual Studio project with MFC support, this error might be caused by this line:
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Make sure to remove it or comment it out.

Problem in including STL header files

I have created Dialog Based application in MFC (VS 2008).When I do "#include" in Dlg.cpp it showing following error.
Is Some Component is missing in the VS 2008 Installation
c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(43) :
error C2665: 'operator new' : none of the 5 overloads could convert all the argument types
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\new.h(85): could be 'void *operator new(size_t,const std::nothrow_t &) throw()'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\new.h(93): or 'void *operator new(size_t,void *)'
1> while trying to match the argument list '(const char [70], int)'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(145) : see reference to function template instantiation '_Ty *std::_Allocate<char>(size_t,_Ty *)' being compiled
1> with
1> [
1> _Ty=char
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(144) : while compiling class template member function 'char *std::allocator<_Ty>::allocate(std::allocator<_Ty>::size_type)'
1> with
1> [
1> _Ty=char
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xstring(2216) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=char
1> ]
1>Build log was saved at "file://c:\Users\Public\Documents\Proj\STL1\STL1\Debug\BuildLog.htm"
1>STL1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any STL includes in your MFC project should be included prior to the definition of DEBUG_NEW. This was a problem in the past (no longer seems to be, as I cannot reproduce it in VS 2010).
// myfile.cpp
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// This will cause the error
#include <vector>
Whereas
// myfile.cpp
// will work OK
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
DEBUG_NEW is a microsoft specific extension that helps to find memory leaks in debug versions of the application. It can be very useful in certain cases.
It's defined in afx.h as:
void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)`
It's helps to provide tracking of memory allocated and when a leak is detected will give you the file and line number where the allocation took place.