Why when I wan to compile the following multi thread merge sorting C program, I receive this error:
ap@sharifvm:~/forTHE04a$ gcc -g -Wall -o mer mer.c -lpthread
mer.c:4:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
ap@sharifvm:~/forTHE04a$ gcc -g -Wall -o mer mer.c -lpthread
mer.c:4:22: fatal error: iostream.h: No such file or directory
#include <iostream.h>
^
compilation terminated.
My program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;
#define N 2 /* # of thread */
int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; /* target array */
/* structure for array index
* used to keep low/high end of sub arrays
*/
typedef struct Arr {
int low;
int high;
} ArrayIndex;
void merge(int low, int high)
{
int mid = (low+high)/2;
int left = low;
int right = mid+1;
int b[high-low+1];
int i, cur = 0;
while(left <= mid && right <= high) {
if (a[left] > a[right])
b[cur++] = a[right++];
else
b[cur++] = a[right++];
}
while(left <= mid) b[cur++] = a[left++];
while(right <= high) b[cur++] = a[left++];
for (i = 0; i < (high-low+1) ; i++) a[low+i] = b[i];
}
void * mergesort(void *a)
{
ArrayIndex *pa = (ArrayIndex *)a;
int mid = (pa->low + pa->high)/2;
ArrayIndex aIndex[N];
pthread_t thread[N];
aIndex[0].low = pa->low;
aIndex[0].high = mid;
aIndex[1].low = mid+1;
aIndex[1].high = pa->high;
if (pa->low >= pa->high) return 0;
int i;
for(i = 0; i < N; i++) pthread_create(&thread[i], NULL, mergesort, &aIndex[i]);
for(i = 0; i < N; i++) pthread_join(thread[i], NULL);
merge(pa->low, pa->high);
//pthread_exit(NULL);
return 0;
}
int main()
{
ArrayIndex ai;
ai.low = 0;
ai.high = sizeof(a)/sizeof(a[0])-1;
pthread_t thread;
pthread_create(&thread, NULL, mergesort, &ai);
pthread_join(thread, NULL);
int i;
for (i = 0; i < 10; i++) printf ("%d ", a[i]);
cout << endl;
return 0;
}
Why when I wan to compile the following multi thread merge sorting C program, I receive this error:
ap@sharifvm:~/forTHE04a$ gcc -g -Wall -o mer mer.c -lpthread
mer.c:4:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
ap@sharifvm:~/forTHE04a$ gcc -g -Wall -o mer mer.c -lpthread
mer.c:4:22: fatal error: iostream.h: No such file or directory
#include <iostream.h>
^
compilation terminated.
My program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;
#define N 2 /* # of thread */
int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; /* target array */
/* structure for array index
* used to keep low/high end of sub arrays
*/
typedef struct Arr {
int low;
int high;
} ArrayIndex;
void merge(int low, int high)
{
int mid = (low+high)/2;
int left = low;
int right = mid+1;
int b[high-low+1];
int i, cur = 0;
while(left <= mid && right <= high) {
if (a[left] > a[right])
b[cur++] = a[right++];
else
b[cur++] = a[right++];
}
while(left <= mid) b[cur++] = a[left++];
while(right <= high) b[cur++] = a[left++];
for (i = 0; i < (high-low+1) ; i++) a[low+i] = b[i];
}
void * mergesort(void *a)
{
ArrayIndex *pa = (ArrayIndex *)a;
int mid = (pa->low + pa->high)/2;
ArrayIndex aIndex[N];
pthread_t thread[N];
aIndex[0].low = pa->low;
aIndex[0].high = mid;
aIndex[1].low = mid+1;
aIndex[1].high = pa->high;
if (pa->low >= pa->high) return 0;
int i;
for(i = 0; i < N; i++) pthread_create(&thread[i], NULL, mergesort, &aIndex[i]);
for(i = 0; i < N; i++) pthread_join(thread[i], NULL);
merge(pa->low, pa->high);
//pthread_exit(NULL);
return 0;
}
int main()
{
ArrayIndex ai;
ai.low = 0;
ai.high = sizeof(a)/sizeof(a[0])-1;
pthread_t thread;
pthread_create(&thread, NULL, mergesort, &ai);
pthread_join(thread, NULL);
int i;
for (i = 0; i < 10; i++) printf ("%d ", a[i]);
cout << endl;
return 0;
}
You should change iostream.h
to iostream
. I was also getting the same error as you are getting, but when I changed iostream.h
to just iostream
, it worked properly. Maybe it would work for you as well.
In other words, change the line that says:
#include <iostream.h>
Make it say this instead:
#include <iostream>
The C++ standard library header files, as defined in the standard, do not have .h
extensions.
As mentioned Riccardo Murri’s answer, you will also need to call cout
by its fully qualified name std::cout
, or have one of these two lines (preferably below your #include
directives but above your other code):
using namespace std;
using std::cout;
The second way is considered preferable, especially for serious programming projects, since it only affects std::cout
, rather than bringing in all the names in the std
namespace (some of which might potentially interfere with names used in your program).
Aliaxandr 9 / 9 / 8 Регистрация: 03.07.2015 Сообщений: 219 |
||||
1 |
||||
01.09.2015, 03:36. Показов 20214. Ответов 12 Метки нет (Все метки)
Привет всем. Беда такая. Все время пока что учил только С, теперь думаю взяться за С++. Помню, когда установил линукс, требовалось докачать еще какие то пакеты данных, потому что на Си не компилировалась простая программа Hello world. Порылся и нашел на одном английскоязычном форуме комманду, которую надо было по просту выполнить в консоли, после чего все заработало. Так вот вопрос как это сделать для С++.
выдает следующую ошибку:
0 |
kalonord 28 / 28 / 5 Регистрация: 27.01.2014 Сообщений: 784 |
||||
01.09.2015, 03:58 |
2 |
|||
Я могу ошибаться, но разве не
? Добавлено через 13 секунд Не по теме: или нет разницы?
1 |
163 / 104 / 14 Регистрация: 17.10.2012 Сообщений: 488 |
|
01.09.2015, 03:59 |
3 |
вместо <iostream.h> напишите <iostream>, а вместо gcc вызывайте g++
1 |
kalonord 28 / 28 / 5 Регистрация: 27.01.2014 Сообщений: 784 |
||||
01.09.2015, 04:04 |
4 |
|||
1 |
9 / 9 / 8 Регистрация: 03.07.2015 Сообщений: 219 |
|
01.09.2015, 04:31 [ТС] |
5 |
спасибо, теперь все заработало
0 |
Aliaxandr 9 / 9 / 8 Регистрация: 03.07.2015 Сообщений: 219 |
||||
14.09.2015, 14:04 [ТС] |
6 |
|||
kalonord, iRomul, тогда вопрос, почему в книге Освой С++ за 21 день вот такой вот пример, который у меня не работает:
Может быть просто нужна другая среда разработки?(я работаю в консоли на линуксе)
0 |
2762 / 1916 / 569 Регистрация: 05.06.2014 Сообщений: 5,571 |
|
14.09.2015, 14:12 |
7 |
Может быть просто нужна другая среда разработки? Нужна машина времени. В прошлом веке такой код действительно компилировался, но в этом уже устарел.
1 |
9 / 9 / 8 Регистрация: 03.07.2015 Сообщений: 219 |
|
14.09.2015, 15:04 [ТС] |
8 |
Renji, может посоветуешь книгу для начинающих, чтобы вот без таких вот траблов???потомучто это не первая книга, где я встречаю такой пример кода для программы, которая по просту печатает Hello world!
0 |
940 / 868 / 355 Регистрация: 10.10.2012 Сообщений: 2,706 |
|
14.09.2015, 15:36 |
9 |
Можешь здесь посмотреть: Литература C++
1 |
28 / 28 / 5 Регистрация: 27.01.2014 Сообщений: 784 |
|
06.10.2015, 01:08 |
11 |
может посоветуешь книгу для начинающих «Как программировать на С++», Дейтелы. Чего так долго на «Hello, World» сидишь? Не по теме: А что за метода освоить С++ за 21 день? Действующая?
1 |
117 / 121 / 42 Регистрация: 25.08.2012 Сообщений: 1,294 |
|
06.10.2015, 01:16 |
12 |
Renji, а что было с пространствами имен тогда? По-моему, неймспейсы были фишкой плюсов еще тогда. Или ради совместимости можно было не указывать?
0 |
2762 / 1916 / 569 Регистрация: 05.06.2014 Сообщений: 5,571 |
|
07.10.2015, 08:12 |
13 |
Renji, а что было с пространствами имен тогда? Тогда еще не придумали убрать всю стандартную библиотеку в std::.
1 |
- Remove From My Forums
-
Question
-
Hi All:
The story so far:
I am having a we bit of trouble running
the most difficult of all C++ programs the
imfamous first C++ program Hello World!
code:
// Simple Hello World Console Application
#include <iostream>
using namespace std;
int main()
{
cout << «nHello World!n» << endl;
return 0;
}And we get:
—— Build started: Project: Hello, Configuration: Debug Win32 ——
Compiling…
hello.cpp
f:program filesmicrosoft sdkbinhellohello.cpp(2) : fatal error C1083: Cannot open include file: ‘iostream.h’: No such file or directory
Build log was saved at «file://f:Program FilesMicrosoft SDKBinHelloDebugBuildLog.htm»
Hello — 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is really wrong this is the second
time I have installed C++ and still is not
running at all.
Answers
-
<iostream>
is normally stored in the C:Program FilesMicrosoft Visual Studio
8VCinclude folder. First check if it is still there. Then
choose Tools + Options, Projects and Solutions, VC++ Directories,
choose «Include files» in the «Show Directories for» combobox and
double-check that $(VCInstallDir)include is on top of the list. -
Check all of your code to make sure you’ve not included iostream.h anywhere. C++ Express edition has no idea what iostream.h is, as it’s not standard C++, this is the first edition to (finally) remove it altogether.