boost::multi_index error "template argument 'x' is invalid" - c++

So I made some kind of database with boost_multi_index like this:
#include <boost\multi_index_container.hpp>
#include <boost\multi_index\ordered_index.hpp>
#include <boost\multi_index\member.hpp>
using namespace boost::multi_index;
using namespace std;
struct list_entry{
int id;
string name;
string* data;
};
typedef multi_index_container<list_entry,
indexed_by<
ordered_unique< tag<int>, member<list_entry, int, &list_entry::id>>, // unique id
ordered_unique< tag<string>, member<list_entry, string, &list_entry::name>>, // unique name
ordered_non_unique< tag<string*>, member<list_entry, string*, &list_entry::data>> // some data associated with the id/name
>
>table;
class Database
{
private:
table music, names;
typedef table::index<int>::type list_id;
typedef table::index<string>::type list_string;
//some more code here
};
and it compiles fine with Visual Studio 2010.
However I wanted to switch my project over to Code::Blocks with MinGW and it seems like he's not so happy with that, this is the compile log:
.\source\db.h|19|error: template argument 3 is invalid|
.\source\db.h|15|error: template argument 2 is invalid|
.\source\db.h|15|error: template argument 1 is invalid|
.\source\db.h|14|error: template argument 2 is invalid|
.\source\db.h|13|warning: 'typedef' was ignored in this declaration [enabled by default]|
.\source\db.h|24|error: 'table' does not name a type|
.\source\db.h|25|error: 'table' does not name a type|
.\source\db.h|26|error: 'table' does not name a type|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|222|warning: 'boost::system::posix_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|223|warning: 'boost::system::errno_ecat' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|224|warning: 'boost::system::native_ecat' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|244|warning: 'boost::asio::error::system_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|246|warning: 'boost::asio::error::netdb_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|248|warning: 'boost::asio::error::addrinfo_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|250|warning: 'boost::asio::error::misc_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\ssl\error.hpp|34|warning: 'boost::asio::error::ssl_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\detail\winsock_init.hpp|116|warning: 'boost::asio::detail::winsock_init_instance' defined but not used [-Wunused-variable]|
||=== Build finished: 7 errors, 10 warnings (0 minutes, 15 seconds) ===|
I'm completely clueless since the error is not any more specific than that and I couldn't find any related problem.
So I hope someone can give me an answer here, if more information is required I will edit it in afterwards.

I was able to solve this issue by using BOOST_MULTI_INDEX_MEMBER like this:
typedef multi_index_container<list_entry,
indexed_by<
ordered_unique< tag<int>, BOOST_MULTI_INDEX_MEMBER(list_entry, int, id)>,
ordered_unique< tag<string>, BOOST_MULTI_INDEX_MEMBER(list_entry, string, name)>,
ordered_non_unique< tag<string*>, BOOST_MULTI_INDEX_MEMBER(list_entry, string*, data)>
>
>table;

Related

Using Legacy Header in C++20 Modules with Clang Compiler

I am trying to write some codes in c++20 standard. I write some simple code in c++ module way. And I write '#include <iostream>' because 'import <iostream>' can not be compiled in clang-15.0.2 compiler. Then I meet compile error.
The code is:
// md.ixx
module;
#include <iostream>
export module md;
export int func() {
return 114514;
}
// main.cpp
#include <iostream>
import md;
using namespace std;
int main() {
auto val = func();
cout<<val<<endl;
return 0;
}
Compile error is:
error: main.cpp:10:5: error: missing '#include <iostream>'; 'cout' must be declared before it is used
cout<<val<<endl;
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\iostream:40:57: note: declaration here is not visible
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT ostream cout;
^
main.cpp:10:16: error: missing '#include <ostream>'; 'endl' must be declared before it is used
cout<<val<<endl;
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\ostream:1000:51: note: declaration here is not visible
basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL endl(
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\ostream:269:9: error: missing '#include <xiosbase>'; 'ios_base' must be declared before it is used
ios_base::iostate _State = ios_base::goodbit;
^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\xiosbase:170:28: note: declaration here is not visible
class _CRTIMP2_PURE_IMPORT ios_base : public _Iosb<int> { // base class for ios
And If I delete #include<iostream> in file md.ixx. It can be successfully compiled.
So is there any solution to using <iostream> in both main.cpp and md.ixx?
Thank you a lot.

'localtime_s': is not a member of 'std' in VS2015

The following C++ code does not compile with VS2015:
#include <time.h>
#include <ctime>
void main()
{
const time_t t = std::time(nullptr);
std::tm tm = {};
std::localtime_s(&tm, &time);
}
The error messages are:
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
t.cpp
t.cpp(10): error C2039: 'localtime_s': is not a member of 'std'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\ctime(17): note: see declaration of 'std'
t.cpp(10): error C2664: 'errno_t localtime_s(tm *const ,const time_t *const )': cannot convert argument 2 from 'time_t (__cdecl *)(time_t *const )' to 'const time_t *const '
t.cpp(10): note: There is no context in which this conversion is possible
What can be a workaround? (using std::localtime() is not an option).
Per [headers]/10
Annex K of the C standard describes a large number of functions, with associated types and macros, which “promote safer, more secure programming” than many of the traditional C library functions. The names of the functions have a suffix of _­s; most of them provide the same service as the C library function with the unsuffixed name, but generally take an additional argument whose value is the size of the result array. If any C++ header is included, it is implementation-defined whether any of these names is declared in the global namespace. (None of them is declared in namespace std.)
Emphasis mine
and table 18 list localtime_s as a member so, because of the above section, we know that it is not defined in namespace std and if it does exist it will be in the global namespace. Try using
::localtime_s(&tm, &time);
and if that still does not work it means your implementation does not support it.

Compilation error upgrading from VS 2008 to VS 2015

I get errors compiling the code below when ugrading from VS 2008 to VS2015. The code is from the com4j project. Help wanted. Thanks!
syntax error: missing ';' before '<' missing type specifier
int assumed. Note: C++ does not support default-int
'array': ambiguous symbol
unexpected token(s) preceding ';'
Code:
// Class to marshal SAFEARRAY to Java multi dimensional array
//
// itemType : array item type
// XDUCER : converter for each array item
template < VARTYPE itemType, class XDUCER >
class ToJavaMultiDimlArrayMarshaller {
typedef array::Array<typename XDUCER::JavaType> JARRAY; // Errors here
typedef SAFEARRAY* NativeType;
typedef jarray JavaType;
What does VARTYPE stand for? Is it a macro? Replacing with class or typename may help

Errors when building OpenCV program on OS X 10.11 via Xcode

I followed these instructions to install and build OpenCV 3 on my Mac: http://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/
I am successfully able to build the sample program (from the command-line) included at the bottom of that article.
I then followed the author's second article here: http://blogs.wcode.org/2014/11/howto-setup-xcode-6-1-to-work-with-opencv-libraries/ - I note that I'm using Xcode 7.2 instead of 6.1, but all of the dialogs and steps seemed to work fine.
...except for building.
With the simple C++ program included in the above article I get these build errors:
Xcode shows 20 errors all coming from cstring.h of the form:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:70:9: No member named 'memcpy' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:71:9: No member named 'memmove' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:72:9: No member named 'strcpy' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:73:9: No member named 'strncpy' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:74:9: No member named 'strcat' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:75:9: No member named 'strncat' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:76:9: No member named 'memcmp' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:77:9: No member named 'strcmp' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:78:9: No member named 'strncmp' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:79:9: No member named 'strcoll' in the global namespace; did you mean 'strtoll'?
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:80:9: No member named 'strxfrm' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:82:9: No member named 'memchr' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:84:9: No member named 'strchr' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:86:9: No member named 'strcspn' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:88:9: No member named 'strpbrk' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:90:9: No member named 'strrchr' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:92:9: No member named 'strspn' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:94:9: No member named 'strstr' in the global namespace
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:98:87: No member named 'strchr' in the global namespace; did you mean simply 'strchr'?
It also gave me errors in the main.cpp file itself:
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:14:14: Invalid operands to binary expression ('ostream' (aka 'int') and 'const char *')
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:29:16: Variable has incomplete type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:30:14: Invalid operands to binary expression ('ostream' (aka 'int') and 'const char *')
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:49:38: No viable conversion from 'cv::Mat' to 'const cv::_InputArray'
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:50:26: Invalid operands to binary expression ('ostream' (aka 'int') and 'const char *')
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:66:17: Variable has incomplete type 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
/Users/me/src/opencv-me-2/OpenCVTest/OpenCVTest/main.cpp:71:14: Invalid operands to binary expression ('ostream' (aka 'int') and 'const char *')
...I was able to eliminate those by adding #include <string> after the #include <stdio.h> line. However the "No member named 'memcpy' in the global namespace" errors still persist.
Update
Here is my minimal working example file:
main.cpp
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace cv;
using namespace std;
int main(int ac, char** av) {
return 0;
}
When I build it, I get the same 20 errors in cstring - which isn't referenced by my main.cpp file directly. Xcode reports the include-trace is:
main.cpp:1
opencv2/imgcodecs.hpp:46
opencv2/core.hpp:54
opencv2/core/base.hpp:53
Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:626

How to declare boost unordered_multimap

I'm trying to use the boost unordered_multimap class and I'm having trouble declaring it. The error follows the code.
#include <iostream>
#include <map> // header file needed for to use MAP STL
#include <boost/unordered_map.hpp>
using namespace std;
int main(void)
{
map<int, map<int, map<int,int> > > _3dstl;
_3dstl[0][0][0] = 10;
cout<<_3d[0][0][0]<<endl;
typedef boost::unordered_multimap<int, typedef boost::unordered_multimap<int, int>> unordered_map;
unordered_map _2d;
return 0;
}
This is the error:
||In function 'int main()':|
|17|error: template argument 2 is invalid|
|17|error: template argument 5 is invalid|
|17|warning: 'typedef' was ignored in this declaration|
|18|error: 'unordered_map' was not declared in this scope|
|18|error: expected ';' before 'location3d'|
||=== Build finished: 4 errors, 1 warnings ===|
Change this line:
typedef boost::unordered_multimap<int, typedef boost::unordered_multimap<int, int>> unordered_map;
To this:
typedef boost::unordered_multimap<int, boost::unordered_multimap<int, int> > unordered_map;
The second typedef is not necessary and a syntax error. Also in C++2003 you have to watch out for >> in template declarations.
Also, please use a different name then unordered_map for the typedef, since this will collide with std::unordered_map if you use using namespace std; (which is IMO a bad practice). A suggestion would be intmap2d or something.