I'm trying to compile CppUTest as a library on IAR Workbench v6.3.3 for the AVR UC3C0512C on Windows 7 x64 but when I compile it, it says that the std namespace is not defined.
Here is the snippet of code where I get the first error, the file is SimpleString.h:
#if CPPUTEST_USE_STD_CPP_LIB
#include <string>
#include <stdint.h>
SimpleString StringFrom(const std::string& other);
The last line contains the std::string and this brings me 190 errors all related to this. The message is:
Error[Pe276]: name followed by "::" must be a class or namespace name
C:\COM\SRC\cpputest35\include\CppUTest\SimpleString.h 143
I have tried using the line below but it does not help:
using namespace std;
Under Library Configuration I select Normal DLIB, I also tried with Full DLIB but IAR can't see the std library
Any ideas please?
According to the IAR manuals
The std namespace is not used in either standard EC++ or in Extended EC++. If you
have code that refers to symbols in the std namespace, simply define std as nothing;
for example:
#define std // Nothing here
Related
I just tried to compile my C++ code and an error appears when I try to do so.
The error appears on line 9
Here are the versions of the gcc and g++ and such
Any help would be appreciated.
Edit:
I am also including Movie.h:
And also Movie.cpp:
https://puu.sh/vb53G/9e9abd1832.png (I was not able to include more than 3 images due to restrictions)
Firstly, in your Movie.h file, you have not included the string header file correctly. It should be:
#include <string> // without the .h extension
error: 'string' does not name a type
Secondly, you have forgotten to add the closing parenthesis of the constructor function of class "Movie". I am assuming that you have added this now, after the edit
As for the marking done by your compiler, you may find the following StackOverflow post helpful:
StackOverflow Post: Unresolved inclusion iostream.
The link is for the Eclipse IDE, but you can find a similar solution for your own IDE (I cannot tell which one you have).
The line under the #include is just a warning (I'm not sure why).
However, the errors are from the "Movie" class:
1. add "using namespace std" on the top of this class.
2. close the parenthesis on the constructor of 'Movie'.
The error messages are fairly clear:
'string' does not name a type
That is, the compiler is unaware of the type string because either:
you have not #include <string> in Movie.h
or you have, but have not brought it into your namespace with a using namespace std;
although why not just refer to it as std::string?
You are missing
#include <string>
I want to use optimization algorithm lbfgs in my project but I do not want to code it myself. So I found Dlib is a good option.
http://dlib.net/compile.html is a good library. I downloaded it. I use windows 7 and visual studio 2012. If I create a new win 32 console project and set property->configuration properties->VC++ Directories->Include Directories to the path of Dlib(dlib-18.10/).
And it works fine which mean I can run examples.
But when I add it to my project. I occur errors.(error : "vector" is ambiguous)
I guess it maybe because the way I include it.
On the document of Dlib, it says,
Again, note that you should not add the dlib folder itself to your compiler's include path. Doing so will cause the build to fail because of name collisions (such as dlib/string.h and string.h from the standard library). Instead you should add the folder that contains the dlib folder to your include search path and then use include statements of the form #include <dlib/queue.h>. This will ensure that everything builds correctly.
But I am not clear what it means above. I googled the Visual Studio search path (Tools / Options / Projects and Solutions / VC++ Directories).. But in my project this is non-editable.
I only use optimization.h in dlib. If I delete 'using namespace dlib;', then ' typedef matrix column_vector;'then the error is matrix is not a template. If I keep 'using namespace dlib;' I have error "vector" is ambiguous`.
#include <dlib/optimization.h>
#include <iostream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------------------
// In dlib, the general purpose solvers optimize functions that take a column
// vector as input and return a double. So here we make a typedef for a
// variable length column vector of doubles. This is the type we will use to
// represent the input to our objective functions which we will be minimizing.
typedef matrix<double,0,1> column_vector;
As the documentation says, include directory should be root directory of the zip you downloaded. Then you include as #include <dlib/vector.h>. However, since vector is defined under namespace of std as well, you should be specifically denote which namespace's STL you will use.
If you want to use std::vector,
#include <vector.h>
then use it as
std::vector<int> stdVar;
Similarly, for dlib,
#include <dlib/geometry/vector
then use it as
dlib::vector<int> dLibVar;
You could also delete using namespace std if you don't use it as frequent as dlib. Then every STL you reference will be dlib. If you want std, just type std::vector.
using namespace std;
using namespace dlib;
#define vector std::vector
use w/ caution
So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?
i am getting this error which i unable to remove in visual studio 2010. i am using one third party library which uses its own definition for "string" Also visual studio's xstring file is there in the folder where it gets installed. now when i am trying to compile code i am getting following error
1>...\xyz.cpp(24): error C2872: 'string' : ambiguous symbol
1> could be 'third party library path\string.h(31)
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
compiler is not able to understand which string definition it should use. How can i remove this error in visual studi 2010. I want the code to use third party string definition.
i tried to set third party path in include directory but still i am seeing this error.
Please help me. Thanks in advance
This is an example of a namespace clash. You probably have in your code:
#include <3rdPartyString.h> // declaration of 3rd party string type
#include <string> // declaration of std::string
using namespace 3rdPartyNamespace;
using namespace std;
...
string myStr; // which string type?
Compiler now doesn't know which string you want to use - one from 3rd party library or STL one. You can resolve this ambiguity by prepending namespace name to the type:
3rdPartyNamespace::string myStr; // if you want to use string from 3rd party library
or
std::string myStr; // if you want to use STL string
Never place using namespace namespace_name; in headers but try to avoid it in source files as well. The best practice is to prepend type name as this does doesn't pollute your current namespace with the other one thus avoiding namespace clashes.
Namespaces were invented to prevent these ambiguities. Make sure you never use using namespace std (that specific using namespace is bad practice anyway, and "std::" isn't that long to type) and it should be fine, just use std::string if you want the standard string.
// Without "using namespace std;"
string foo; // string from third party library used.
std::string bar; // string from standard library used.
The two definitions of string are clashing with each other so the compiler doesn't know what to use so you need a way to differentiate the two and that's where namespaces come in.
You can use the namespace that your third party string uses when referring to that string as the errors you are showing implies you have using namespace std in your code.
I ran into this issue recently by adding a Class Library project to a very large solution that included an in-house String library, the using namespace System; in the pre-generated header was causing the ambiguity.
There is System.String. If not intended to use, make sure to indicate otherwise. Example System::String, or Custom::String
I am trying to get a get the boost library program_options working on a simple windows console library.
I have linked in the library
C:\Program Files\boost\boost_1_40\lib\libboost_program_options-vc90-s-1_40.lib
Included the header files
#include <boost/program_options.hpp>
#include <boost/program_options/config.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/detail/cmdline.hpp>
#include <boost/program_options/detail/parsers.hpp >
Defined _WIN32 (But I don't think it is required.)
And I still keep getting the
Error 1 error C3861: 'split_winmain': identifier not found
It should be so simple but I can't get it to work. Can anyone tell me what I need to do here.
Joseph Shanahan
That function is declared in the boost::program_options namespace. If all you do is use its name alone, the compiler doesn't know what you're talking about. You have a few options:
Use the fully qualified name when you call it:
boost::program_options::split_winmain(...);
Tell the compiler which function you mean:
using boost::program_options::split_winmain;
split_winmain(...);
Bring the entire namespace into the current scope:
using namespace boost::program_options;
split_winmain(...);
Make a namespace alias:
namespace po = boost::program_options;
po::split_winmain(...);
I prefer the last one.
Do not define the _WIN32 macro; the compiler will do that for you when it's appropriate.