I wrote the following C++ program, but at the line where I used out_stream.open(), it keeps telling me that there are errors about "unknown typename 'out_stream'" and "Expected unqualified-id".
I am new to C++ and I think I just copied down the lines from my textbook, so I can't figure out where it's wrong. Please bear with me if it is a really simple mistake.
Here's my code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/math/distributions.hpp>
std::ofstream out_stream;
out_stream.open("output.txt");
int main()
{
std::cout<<"hello world!";
return 0;
}
You cannot to this
out_stream.open("output.txt");
outside of a function. Put it inside the main().
int main()
{
out_stream.open("output.txt");
std::cout<<"hello world!";
return 0;
}
Related
this is the program
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
getch();
}
fatal error: conio.h: No such file or directory #include <conio.h>sdfs
getch return a value
as follows
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
char a = getch();
}
I am having problems running functions within a custom class. Here is my code:
ROBOTSTRUCTURE.H
#pragma once
#include "math.h"
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
using namespace std::chrono;
class RobotStructure
{
bool faceTrackingEnabled;
public:
RobotStructure();
~RobotStructure();
bool testFunction(string input);
ROBOTSTRUCTURE.CPP
#include "RobotStructure.h"
RobotStructure::RobotStructure()
{
faceTrackingEnabled = true;
}
RobotStructure::~RobotStructure()
{
}
bool RobotStructure::testFunction(string input)
{
cout << input << endl; //THIS DOES NOT WORK, When using debugger shows that the entire class "Robot Structure" as unable to read memory
return true;
}
MAIN
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <fstream>
#include "RobotStructure.h"
using namespace std;
int main()
{
RobotStructure *mybot= new RobotStructure();
mybot->testFunction("test");
return 0;
}
If a set a breakpoint in main, my class is initialized correctly and everything is fine. As soon as a call the "test function" the class falls out of memory within the test function.
When it goes back to main just before return 0; the class is also out of memory. As if the pointer is deleted somehow. Can someone please explain what is happening and what did I do that is wrong?
You declared the function as bool testFunction(string input) but the function is not returning anything. This leads to undefined behavior in C++.
In addition your example wouldn't compile (you declared an argument input but you are using intput).
Your testFunction does not use any class members. If you compile this code with optimization enabled ("Release"-Configuration in VisualStudio, -O2 on gcc), the compiler might render some variables (like this) "undebuggable".
I have a problem with the next code, I get the error: invalid type argument of unary '*' (have 'int'). If I write int *content that allows the code to run, but I have to write int=content and change the code *((ptab->content)+pC1+17), I tried but I can't fix the error.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
struct box{
int content;
};
struct box *ptab;
int pC1=5;
int main (){
ptab=new struct box[64];
if (*((ptab->content)+pC1+17)==0) {
pC1=pC1+17;
}
cout<<pC1<<endl;
}
I have to pass from pointers to poninters to struct arrays, this code is an example, because the original code has 23000 lines.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *box;
int pC1=5;
int main (){
box=new int[64];
if (*(box+pC1+17)==0){
pC1=pC1+17;
}
cout<<pC1<<endl;
}
With *((ptab->content)+pC1+17), an easier way to say it is ptab[pC1+17].content [the latter compiles and produces 22 as output]. Did you mean that or ptab->content + pC1 + 17 [which produces 5]?
How I can sent string variable into function in other file?
main.cpp:
#include <iostream>
#include <conio.h>
#include <string>
#include "headers.hpp"
using namespace std;
int main()
{
string a;
cout<<"Type:"<<endl;
cin>>a;
other(a);
getch();
return( 0 );
}
headers.hpp:
#ifndef HEADERS_HPP
#define HEADERS_HPP
void other(string a);
#endif
function.cpp:
#include "headers.hpp"
#include <iostream>
#include <math.h>
using namespace std;
void other(string a){
cout<<a;}
I don't know why it doesn't work. Do you know solution?
If you are saying it does not compile, you have to move the #include <string> from main.cpp into headers.hpp
I use QT creator and Xcode 4.63 (osx mav)
I just call .set(....) and got following error
no member named 'set' in 'std::vector<int, std::allocator<int> >
seem like something wrong with lib , since I'm new to QT creator i still got no idea how to make it right. (I'm new to c++)
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "console.h"
#include "filelib.h"
#include "grid.h"
#include "gwindow.h"
#include "simpio.h"
#include "vector.h"
using namespace std;
int main() {
vector<int> myVector(10,0);
fstream mystream;
string getStream;
while(getline(mystream,getStream)) {
getline(mystream,getStream);
if(stringToInteger(getStream)>=0 && stringToInteger(getStream)<=9) {
myVector.set(0,(myVector[0]+1))
};
}
return 0;
};
The "set" method doesn't exist for vectors in STL. You could use the [] operator if you want to change a value:
myVector[0] = myVector[0] + 1;
or even
myVector[0]++;