I have a program written in C++, consisting of 6 classes. Somebody else, who has left, using a version of visual C++, wrote the program. However, the program has no GUIs and is supposed to run in batch mode, generating lots of data (we are talking 100,000+ ASCII files). So, we are thinking that in long term we’ll run it under UNIX.
I have a MacBook air running OS X 10.9.4 and I loaded Xcode 5.1.1
I have quickly examined the 13 files making up the source code and removed various #include "StdAfx.h" statements. Nothing else seems to be non-ANSI compliant. I tried to compile. I get 4 instances of the following error
Unknown type name ‘vector’
Indeed I have various declarations of the type
vector<VariableName*> VariableInstance
In my previous experience something like #include <vector.h> should work. However, it does not work here.
What may I be doing wrong?
normally you do
#include <vector>
then usually you'd do std::vector<blah>
however, you can put the following statement before you start using vector, but generally not recommended...
using namespace std;
and then you can do vector<blah>
Related
This question already has answers here:
How do I set up Visual Studio Code to compile C++ code?
(14 answers)
How does one set up the Visual Studio Code compiler/debugger to GCC?
(7 answers)
Closed 1 year ago.
So, I created a new project, here is my code:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
For whatever reason, #include has an error, and the lightbulb won't give me any solutions. I have the code runner and c++ extensions installed.
I'm getting errors left and right for no reason including:
A red line under #include
Running the code doesn't work and says "'g++' is not recognized as an internal or external command,operable program or batch file.
" which doesn't even exist.
And so much more.
It depends on how you start off learning C++. At the very least your starter sandbox code should look like this:
#include <iostream>
int main()
{
return 0;
}
and you should have no problems.
When it comes to introducing input/output operators, the use of using namespace std; is a polarizing one.
Method 1:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Some people don't want you using it at all because ... what if you decide to create something called "namespace" yourself? In fact, this school of thought is very fearful of global things for reasons that escape me. Then you would have to take it out and rely on the traditional method ...
Method 2: eschewing namespace and using std::. You would have to use std::cout for printing text output to the prompt and std::cin for prompting input from the user.
Also, it looks like you are encountering multiple problems, so try attacking them one at a time. I haven't worked with g++ in a long time, so it sounds like you should first do some research on how to compile and run code with g++.
I am doing some arduino development using cpp and h files and I am having some troubles using string with them. Currently I have
#include <string>
at the top of both the cpp and the h file. When I do that it gives me the error:
string: no such file or directory
If I go into the h file and change it to
#include <string.h>
then it gives me the error:
std::string has not been declared
Anytime I use the string I use: std::string to declare it. I am not using namespace std and these files were working together fine before I started to try to use string. I am new to C/C++ so I appreciate any help. Thanks!
In short, there is a way to use std::string with the Arduino.
TL;DR:
link to the arduino STLv1.1.2
NOTE
Please note that currently the harrdwareserialstream class provided by this STL should be considered broken (as per my testing, with version 1.6.5 of the IDE, and possibly anything after 1.0.6). therefore, you can't use
hardwareserialstream << "Hi there person number " << (int)i
and so on. it seems to no longer work due to taking a reference to the serial port it would interact with rather than a pointer - in short, continue using
Serial.print("Hi there person number");
Serial.print((int)i);
Lastly the serial classes don't know what a std::string is, so if using them, give it std::string.c_str() instead
Background
As McEricSir says in the comments, the arduino does provide its own string class, though i have found it to have problems related to memory leakage, which eventually ate all of the memory i had and the program stopped running - though this was in the arduino IDE v 1.0.5, it may have been fixed since then.
I had the same problem, and found someone who had created a version of the STL for the arduino (props to Andy Brown for this) which is a cutdown version of the SGI STL. it provides std::string, std::vector and a large amount of the STL to the arduino.
there are some things to be aware when using it though; if you have a board with very little memory, you will fill it quite quickly using the smart containers and other advanced features.
Using the Library
To use the library, you'll need to read the article, though I'll summarise the main points for you here:
Installation
simply extract the library to (assuming you are using the standard Arduino IDE) hardware\tools\avr\avr\include folder and you are good to go.
Using It
To actually use the new libraries, you need to include 2 additional things as well as the library you wanted.
firstly, you need to include the header iterator BEFORE any libraries that come from this STL - and in every file you reference the STL in.
Secondly, you also need to include the file pnew.cpp to provide an implementation of the new operator for the STL to work with.
Lastly, include any header files as you would normally.
to make use of the types gained from them, don't forget the the std:: namespace notation for them. (std::string and so on)
Bugs with it
Since Andy posted the library, there have been two bugs (that i'm aware of).
The first one Andy himself rectifies and explain in the blog post:
The compiler will spit out a typically cryptic succession of template errors, with the key error being this one:
dependent-name std::basic_string::size_type is parsed as a non-type,
but instantiation yields a type c:/program files (x86)/arduino-1.0/
hardware/tools/avr/lib/gcc/../../avr/include/string:1106: note:
say typename std::basic_string::size_type if a type is meant
Basically the STL was written a long time ago when C++ compilers were a little more forgiving around dependent types inherited from templates. These days they are rightly more strict and you are forced to explicitly say that you mean a type using the typename keyword.
Additionally, he provides the updated version for you to grab.
Lastly, there are reports in the comments about a bug in the newer versions of the IDE pertaining to the vector class where the compiler complains about the use of _M_deallocate without a prepending this->, which you can fix if you search for them inside the vector class
For your convenience
As i use this quite frequently, i've packaged up the current version, which can be found here (this includes both the fixes i have commented on)
Lastly
When using this, make sure to keep an eye on your free memory, and to that end i recommend the excellent class MemoryFree Library found here
on a side note if you #include<string> inside the header you won't need to include it in the relevant .cpp file
I do not understand why this C++ program will not build and run using Codeblocks and the GNU CC compiler.
#include <iostream>
using namespace std;
int main()
{
string firstName;
firstName = "Brandon";
cout << "Hello " << firstName << endl;
return 0;
}
Want to use strings? You'll need to #include it.
#include <string>
In general, when you use a class, function or such, you should look up (unless you have memorised it from using it many times) what header the class or type should.
For example, this page is the first hit in google for C++ string:
http://www.cplusplus.com/reference/string/string/
On this particular page, it shows <string> on the left-hand side of the page, to indicate that <string> is the header for this particular class.
Occassionally it is possible to use a class or function without including its correct header. It is, however, very bad form to RELY on such behaviour (unless you are in direct control of that header, of course). It appears that your example, when it does #include <iostream>, is indeed relying on that also doing #include <string>. It would then appear that this is not the case in your combination of OS & Compiler, hence you get an error.
And unfortunately, the error messages when you use << for output of unknown types or otherwise make a mistake in that type of code, can be extremely unhelpful and often quite verbose as well (e.g. I missed out the << between the actual output stream and some text the other day, and I got a good page full of error messages, none of which made much sense at all - but it gave me which line was wrong, and that was enough for me to eventually spot the error).
Let me start by saying, I've never done raw C++ coding before. I've used a C-based language inside of a premade engine, so I understand in a general sense how the syntax works.
I know every script requires a header file that declares most of the basic functions to operate properly, and for C++, this header would be iostream. My issue is the general "Hello world" issue. I've browsed for a few days but never found an appropriate solution, nor does my situation seem to be the same as anyone else's. Here's my script.
#include <iostream.h>
using namespace std;
int main(){
cout<< "Hey" << ;
return 0 ;
}
So yeah. This should compile appropriately. But the issue comes from the first line, which is the #include.
Here is the error message I get when trying to build it:
1>------ Build started: Project: compiler test, Configuration: Debug Win32 ------
1>Compiler test.cpp
1>Compiler test.cpp(1): fatal error C1083: Cannot open include file: 'iostream.h': No such
file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This would normally tell me that the header isn't in the proper directory, but when I check the /VC/include directory, iostream is just sitting there. Almost seems like Visual C++ is refusing to acknowledge that it exists. Anyone have a solution to this?
first thing to try is #include <iostream>
There is no .h in the C++ headers
(a) - when asking questions please include the operating system and compiler you are using. The behavior differs between platforms and, sometimes, between releases of the compilers. FRom the looks of the messages, it looks like the compiler is a version of Microsoft's Visual C++.
(b) general usage is to use " (quotes) surrounding header files with .h and angle brackets (< >) around headers without the .h. Note that this is a general usage. The standards governing the two languages (C and C++) actually rule. And they say it's "implimentation-defined" (see this relevant StackOverflow question for excruciating detail). Both usages will bring in the function headers, constants and code to provide the requested functionality. Under MS C++ the iostream header provides linkages to the cout, cin, clog and cerr functions, and the equivelant wide character stream functions wcout, wcin, wclog and wcerr ("wide character" support is, broadly, a way to accomodate character representations that are outside the common ASCII or EBCDIC universe)
(3) Once the header file is being brought in, you will get an error, as Sean noted, on the "hey" statement, because the trailing "<<" is expecting anothjer operand, either another string or a variable of some sort (the "endl" is used to close off the output string you have been building and display it. This is also sometimes referred to as "flushing the buffer.")
(4) if you look through the answer/question database here you will find some lively discussion on the use of "using namespace std" as opposed to specifying the usage for each function in the namespace that you want to use. such as "using std::cout;"
I'm attaching a minimally reworked version of your program below (even showing that you could surround the iostream #include by quotes, which should start a a little flame war):
c:\gpp_code>type text3.cpp
#include "iostream"
using namespace std;
int main()
{
cout<< "Hey" << endl ;
return 0 ;
}
c:\gpp_code>text3
Hey
c:\gpp_code>
I can't get my following c++ program compiled in Visual Studio 2010. I already have a working build of the same code so I know the code is correct. But I have no idea with what or how it was compiled.
So I would appreciate if someone could just copy the code and try to compile it in VS 2010.
Code:
http://codepad.org/4VtrVBdK
new:
Ok, I did editing according to the comments below. Now the only problems that seem to have remained are related to calls to the overloaded functions. So how to go about it?
so I know the code is correct
What you "know" is false. The code is wrong in many ways. Here is but one example:
for(unsigned int i=0;i<GPNO;i++) //SORTING ACCORDING TO FITNESS
for(unsigned int j=i+1;j<GPNO;j++)
if((gp[i]->fitness)>(gp[j]->fitness))
{
gp[i]->mycopy(tmp);
gp[j]->mycopy(gp[i]);
tmp->mycopy(gp[j]);
}
for(i=1;i<=no;i++)
{
gp[i]->mycopy(gp[GPNO-i]);
}
In the second for loop, i is undeclared. I suspect the original compiler was VC6, which allowed this.
Another problem is the way you're calling pow. You call it with macros (which are patently evil for this purpose), for instance:
pf[i].frq+=(unsigned int)pow(2,2*PF-1);
And the compiler doesn't know which version of pow you had in mind. Case in point for macros being evil for this purpose. Do this:
pf[i].frq+=(unsigned int)pow(2.0,2*PF-1);
Or better yet, get rid of the macros.
Another example of your code being wrong:
#include "stdlib.h"
#include "conio.h"
#include "math.h"
None of these includes are part of the Standard. If you can get them to compile, its only because your compiler was anticipating your mistake. But it's still a mistake.
Looks like you're missing using namespace std;