std::string is ambiguous, whenever including non-standard libraries like boost - c++

JSONParser.h
#ifndef UTILITY_CLASSES_JSONPARSER_H_
#define UTILITY_CLASSES_JSONPARSER_H_
#include <iostream>
#include <string>
#include <boost/property_tree/json_parser.hpp>
namespace Parsers {
class JSONParser {
boost::property_tree::ptree pTree;
public:
JSONParser();
virtual ~JSONParser();
void setJSON(char* JSON);
};
} /* namespace Parsers */
#endif /* UTILITY_CLASSES_JSONPARSER_H_ */
JSONParser.cpp
/*
* JSONParser.cpp
*
* Created on: 20-May-2016
* Author: arjun
*/
#include "JSONParser.h"
namespace Parsers {
JSONParser::JSONParser() {
// TODO Auto-generated constructor stub
}
JSONParser::~JSONParser() {
// TODO Auto-generated destructor stub
}
void JSONParser::setJSON(char* JSON){
std::string temp;
pTree.put("foo", "bar");
pTree.put("foor", "bawr");
std::ostringstream buf;
write_json (buf, pTree, false);
}
} /* namespace Parsers */
Defined above, are a cpp-header pair I've written for a JSONParser. However, an error is thrown when I try to use std::strings:
std::string is ambiguous.
It seems this happens whenever I include boost/property_tree/json_parser.hpp, i.e. whenever I delete the include for it, there is no error.
The same happens when I include something off of GeographicLib.
I'm on Ubuntu 16.04, my libraries are in usr/local/lib, and includes are in usr/local/include
The error is as follows:
Description Resource Path Location Type
'std::string' is ambiguous '
Candidates are:
' JSONParser.cpp /PSO-PathFinding/Utility Classes line 14 Semantic Error
Programming on Eclipse, using cdt.

Resolved. Not a C++ issue, or boost library issue. Seems to be a problem with the current eclipse workspace I'm working with.

Related

Why g++ reported an error in C++ Standard Library in MacOS GDK?

I encountered a weird error when building my code:
Before I show you what the error was, I'd like to show you what the code is doing. This is a node class for my little cmd project, each node is a file/folder. I wrote a function to show the info of the node.
// Node.h
#include <string>
#include <memory>
using namespace std;
typedef bool NODE_TYPE;
#define FILE true
#define DIR false
class Node
{
private:
string _node_name;
NODE_TYPE _node_type;
string _create_date;
string _create_user;
weak_ptr<Node> _parent;
shared_ptr<Node> _sibling;
shared_ptr<Node> _child;
public:
/*Constructors, Destructors and other functions*/
const string toString();
};
// Node.cpp
#include "Node.h"
#include <string>
#include <sstream>
using namespace std;
const string Node::toString()
{
ostringstream buffer;
buffer << ... // the data member of Node
return buffer.str();
}
I wrote a test in main.cpp, and when I say g++ Node.cpp main.cpp -Wall -std=c++11, here's the error:
In file included from Node.cpp:6:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:184:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:163:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:40:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale.h:93:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale/_stdio.h:32:33: error: expected expression
int fprintf_l(FILE * __restrict, locale_t __restrict, const char * __restrict, ...)
Errors like this are 21 more, I guess there was something wrong with SDK, so I reinstalled it, however still no work. Could anyone help me?
I'd like to thank all comments below my question, they are really helpful.
In C standard library, FILE is defined as a structure to which stores the info of a file opened by the program. So my own macro definition leads to wrong macro expansion.
Here I'd like to reference the suggestion by Scott Meyers: For simple constants, prefer const objects or enums to #defines.

Boost serialization exception error

I have recently ran into an error when compiling my program with the boost serialization library.
I have read across many forums that I need to link the boost serialization library (-lboost_serialization). Which, I did.
However, there is one error that I am getting on the code:
/usr/local/boost_1_64_0/boost/serialization/throw_exception.hpp:36: undefined reference to `boost::archive::archive_exception::archive_exception(boost::archive::archive_exception const&)'
So I thought, what if I have to also include the exception library?
So I added this into the libraries for my compiler settings:
-lboost_exception
However, now it is saying that it is unable to find the boost_exception library. Which is interesting because I am able to see it in the file system!
I am wondering if I am doing the right thing or am I forgetting to add something to my compiler settings? Any help will be much appreciated!
Edit:
Here is the code:
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
#endif
// boost/throw_exception.hpp
//
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/config.hpp>
#ifndef BOOST_NO_EXCEPTIONS
#include <exception>
#endif
namespace boost {
namespace serialization {
#ifdef BOOST_NO_EXCEPTIONS
inline void throw_exception(std::exception const & e) {
::boost::throw_exception(e);
}
#else
template<class E> inline void throw_exception(E const & e){
throw e;// Error occurs here
}
#endif
} // namespace serialization
} // namespace boost
Here is a code sample where the breakage occurs
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
#include "boost/serialization/vector.hpp"
#include <string>
#include <fstream>
int main()
{
std::ofstream ofs("/home/phillip/test.txt");
std::vector<std::string> tests;
boost::archive::text_oarchive oa(ofs);
oa << tests;
}
I've seen this happen when linking to the boost library when compiling with a different compiler/version/flags.
E.g. many Boost Serialization answer programs wouldn't link when compiling with Clang on Coliru whereas they'd run fine when compiling with Gcc

linux eclipse c++ local namespaces and "std::"

I am trying my first c++ project and am starting small, I think. I am using Eclipse Luna and am trying to take the defaults for build env everywhere that I can. I suspect that the following is vary naive code but you gotta start somewhere.
I will eventually have 4 co/sub projects in this:
Commandline interface to a static lib
".so" lib that will be dynamically loaded into a different, thied party app and uses the static lib
A static library, mentioned above, that does the back end work of the combined app.
A utility class (for now, maybe a small lib later) for utility classes and functions common to the other sub projects.
There are a ton of questions on best practices, namespaces, etc that I would like to ask but I'll keep this short.
I have the following c++ header file:
/*
* Utilities.h
*
*/
#ifndef UTILITIES_H_
#define UTILITIES_H_
// A
namespace UserTrackingUtilities {
// B
#include <string>
#include <exception>
using namespace std;
class MyException: public std::exception {
public:
MyException(std::string ss) : s(ss) {
}
~MyException() throw () {
} // Updated
std::string s;
const char* what() const throw () {
return s.c_str();
}
};
}
#endif /* UTILITIES_H_ */
This is an exception utility (found in a different StackOverflow thread) that I am wrapping in a namespace of my own -- I think.
Eclipse is showing several issues with this header file. I'll start with one: it doesn't like the std::string construct. It doesn't matter if I put the #includes and/or the using statements at point A or B.
I've also tried this with the Linux GCC and ADT tool chains.
Pointers and advice welcome.
#include <string>
#include <exception>
Should be before
namespace UserTrackingUtilities {
BTW:
If you use using namespace std, you can write string instead of std::string.
But I suggest not to use using namespace std to avoid name conflicts and ambiguities.
Update:
Here is a minimal working example:
#include <iostream>
#include <string>
#include <exception>
namespace UserTrackingUtilities {
class MyException: public std::exception {
public:
MyException(std::string ss) : s(ss) {
}
~MyException() throw () {
} // Updated
std::string s;
const char* what() const throw () {
return s.c_str();
}
};
}
int main()
{
UserTrackingUtilities::MyException ex("Hello World");
std::cout << ex.what() << std::endl;
return 0;
}

How to call managed C++ methods from Un-managed C++

PLEASE SEE UPDATE BELOW
(RESOLVED) Also I have extended this into a second question here Implement a C# DLL COM File In Unmanaged C++ Program
I have researched this to the end of the internet without finding a real, understandable, human example of how to do this.
I have a C# DLL that encrypts and decrypts text.
I don't want to / don't have the intellectual capability to rewrite this in C++ un-managed code. So instead I created a C++/CLR class that interfaces with the C# dll.
NOW I need to know how to call the managed C++ from my unmanaged code.
Here is my managed code and it is verified that it works
// clrTest.cpp : main project file.
#include "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
using namespace cSharpRiJHarn;
using namespace System;
String^ Encrypt(String ^s)
{
return RijndaelLink::encrypt(s);
}
String^ Decrypt(String ^s)
{
return RijndaelLink::decrpyt(s);
}
int main()
{
//Console::WriteLine(Encrypt("It Works"));
//Console::WriteLine(Decrypt(Encrypt("It Works")));
//Console::ReadLine();
return 0;
}
Now ONCE AGAIN I HAVE researched this.
I have seen allllllll the bad/overly complicated explanations
I know I need to use something called COM or Interop
I don't know how this works and I am just looking for a very simple explanation.
Thanks for the help.
UPDATE
I have turned the C# DLL into a COM File
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cSharpRiJHarn
{
[Guid("GuiD CODE REMOVED")]
public interface DBCOM_Interface
{
[DispId(1)]
String encrypt(string s);
[DispId(2)]
String decrpyt(string s);
}
[Guid("GuiD CODE REMOVED"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface DBCOM_Events
{
}
[Guid("GuiD CODE REMOVED"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(DBCOM_Events))]
public class RijndaelLink : DBCOM_Interface
{
public String encrypt(String s)
{
return Rijndael.EncryptString(s);
}
public String decrpyt(String s)
{
return Rijndael.DecryptString(s);
}
}
}
Now I am just need to know how to implement this in unmanaged C++...
I have tried both adding just the files to the C++ project and also adding the entire cSharpRiJHarn Project to this solution. Neither work.
#import "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <iostream>
//using namespace cSharpRiJHarn;
int main(){
cSharpRiJHarn::RijndaelLink::encrypt("It works");
char ch;
std::cin>>ch;
return 0;
}
This is one of the errors I am getting.
Error 6 error C2653: 'cSharpRiJHarn' : is not a class or namespace
name
and
Error 8 IntelliSense: cannot open source file
"C:/.../.../Documents/Visual Studio
2010/Projects/unmannagedCPPExample/unmannagedCPPExample/Debug/cSharpRiJHarn.tlh" c:......\documents\visual
studio
2010\projects\unmannagedcppexample\unmannagedcppexample\unmannagedcppexample.cpp
You could use the cool C++ Marshaling library provided by Microsoft, something like this:
#include "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "msclr\marshal_cppstd.h" // marshaling library
using namespace cSharpRiJHarn;
using namespace System;
using namespace msclr::interop; // marshaling library
std::wstring Encrypt(std::wstring s)
{
return marshal_as<std::wstring>(RijndaelLink::encrypt(marshal_as<String^>(s)));
}
std::wstring Decrypt(std::wstring s)
{
return marshal_as<std::wstring>(RijndaelLink::decrypt(marshal_as<String^>(s)));
}
First, your methods receive and return a String^ which is a managed object. Unmanaged code does not know this type, and cannot create such object. So, you will need to wrap the function call such that the function marshal the managed type to something that the unmanaged code can understand.
After that, you can add the DllExport attribute to the managed method, as discussed here.

swig generated code, generating illegal storage class for python wrapper of C++ API

I'm trying to take the approach of using swig with the main header file. It seems like swig will work doing this, but I've run into some problems. I asked a first question about it here on stackoverflow and while I haven't yet been successful, I've made enough progress to feel encouraged to continue...
So now, here's my interface file:
/* File : myAPI.i */
%module myAPI
%{
#include "stdafx.h"
#include <boost/algorithm/string.hpp>
... many other includes ...
#include "myAPI.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
using boost::format;
using namespace std;
using namespace boost::algorithm;
using namespace boost::serialization;
%}
/* Let's just grab the original header file here */
%include "myAPI.h"
As far as I can tell swig runs just fine. However, in the generated code it produces numerous definitions like this one:
SWIGINTERN int Swig_var_ModState_set(PyObject *_val) {
{
void *argp = 0;
int res = SWIG_ConvertPtr(_val, &argp, SWIGTYPE_p_MY_API, 0 | 0);
if (!SWIG_IsOK(res)) {
SWIG_exception_fail(SWIG_ArgError(res), "in variable '""ModState""' of type '""MY_API""'");
}
if (!argp) {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in variable '""ModState""' of type '""MY_API""'");
} else {
MY_API * temp;
temp = reinterpret_cast< MY_API * >(argp);
ModState = *temp;
if (SWIG_IsNewObj(res)) delete temp;
}
}
return 0;
fail:
return 1;
}
Visual Studio 2010 complains with an error for each of these code blocks. The error is based on the temp var:
2>c:\svn\myapi\myapi_wrap.cxx(3109): error C2071: 'temp' : illegal storage class
I tried just to add a global declaration of this variable as an int to the swig generated _wrap.cxx file, but it didn't not work. (clearly a naive approach...).
Does anyone have some insight as to what I need to do to avoid this error?
Thanks in advance!