I am trying to enter the simple "Hello World!" code, but cout and endl are undefined.
#include<iostream>
#include "stdafx.h"
int main()
{
std::cout << "Hello World!" << std::endl;
}
It turns out the errors: "'cout': is not a member of 'std', note: see declaration of 'std', 'cout': undeclared identifier", and the same with endl.
Please Help.
You are missing a space in the #include statement:
#include<iostream>
This is the correct way:
#include <iostream>
Full code:
#include <iostream>
#include "stdafx.h"
int main()
{
std::cout << "Hello World!" << std::endl;
}
Related
I have a issue, where I need the GUI from QT Designer to give values to a separate program running from terminal where the values from the GUI are "printed" onto the terminal interface (Linux with GCC compiler)
I have researched pthreads, but their application examples are limited to In-Application uses. The code in my main file is as follows:
#include "main_window.h"
#include <QApplication>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void *thr_func(void *thread_id)
{
int tid = thread_id;
pthread_mutex_lock(&lock_x);
cout << "thread" << tid << end1;
cout << xValue << end1;
cout << yValue << end1;
cout << zValue << end1;
pthread_mutex_unlock(&lock_x);
}
int main(int argc, char *argv[])
{
pthread_create(thread_1, NULL, thr_func, NULL)
while(true)
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
pthread_exit(NULL);
}
**Note that xValue, yValue, and zValue already stream to a textfile from the QT application. I am adapting the application for terminal running and control.
I was trying to make a program write 8 words to a line after a user enter their sentence.Its only printing words that have been typed in and i don't have a clue how to make it type 8 words to a line.
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
vector<string> sentence;
string sente = "";
void print(string, string);
template<typename T>
void print(vector<T>& v, string)
{
cout << "Enter your sentence " << endl;
getline(cin, sente);
sentence.push_back(sente);
for (auto const elem: sentence)
{
cout << elem;
}
}
int main()
{
print(sentence,sente);
}
Using global variables is generally not a good practice.
Also you don't need a extra vector for your use case.
Take a look at the following code, where you can smartly make use of istringstream for your use case:
#include <iostream>
#include <string>
#include <sstream>
void print()
{
std::string sente;
std::cout << "Enter your sentence " << std::endl;
getline(std::cin, sente);
// Used to split string around spaces.
std::istringstream ss(sente);
int wordCountPerLine = 0;
int requiredWordsPerLine = 8;
// Traverse through all words
do {
// Read a word
std::string word;
ss >> word;
// Print the read word
std::cout << word << " ";
wordCountPerLine++;
if(wordCountPerLine % requiredWordsPerLine == 0){
std::cout<<std::endl;
wordCountPerLine = 0;
}
// While there is more to read
} while (ss);
}
int main()
{
print();
}
Feel free to ask any doubts.
I have a question for boost.spirit.qi string parser. When I want parse string value to std::string attribute, like bellow:
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/assert.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
template <typename P, typename T>
void test_phrase_parser_attr(
char const* input, P const& p, T& attr, bool full_match = true)
{
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::ascii::space;
char const* f(input);
char const* l(f + strlen(f));
if (phrase_parse(f, l, p, space, attr) && (!full_match || (f == l)))
std::cout << "ok" << std::endl;
else
std::cout << "fail" << std::endl;
}
int main()
{
std::string str("abc");
test_phrase_parser_attr("cba", string("cba"), str);
std::cout << str << std::endl;
return 0;
}
The output:abccba
But I want the program output "cba", How can I do?
Note that your str was initialized with "abc". Parser does not clear the string, just appends its output.
Pass in an empty string and you will get back what you want.
What will I get from QThread::getCurrentThread(), if it is called from non-Qt thread?
Thanks!
QThread is just a wrapper, behind the scene it uses native threads.
QThread::currentThread creates and initialises a Q(Adopted)Thread instance if it doesn't exist yet.
In case of unix it uses pthreads.
#include <iostream>
#include <thread>
#include <pthread.h>
#include <QThread>
#include <QDebug>
void run() {
QThread *thread = QThread::currentThread();
qDebug() << thread;
std::cout << QThread::currentThreadId() << std::endl;
std::cout << std::this_thread::get_id() << std::endl;
std::cout << pthread_self() << std::endl;
thread->sleep(1);
std::cout << "finished\n";
}
int main() {
std::thread t1(run);
t1.join();
}
Output:
QThread(0x7fce51410fd0)
0x10edb6000
0x10edb6000
0x10edb6000
finished
I see that there is initialisation of Qt application main thread:
data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
So there might be some side effects.
I'd advise not to mix QThread with non-Qt threads.
So I am trying to use pthread libraries for Visual C++(2012) and I get this error error C4716: 'print_message' : must return a value
Here's the code
#include "stdafx.h"
#include <iostream>
#include "pthread.h"
using namespace std;
void* print_message(void *)
{
cout << "Threading\n";
}
int main()
{
pthread_t t1;
pthread_create(&t1, NULL, print_message, NULL);
cout << "Hello";
void* result;
pthread_join(t1,&result);
return 0;
}
Add return NULL; to print_message. I'll bet you need to name the argument too.