45 / 25 / 16
Регистрация: 14.04.2011
Сообщений: 623
1

Test.cpp:19: error: ‘mysql’ was not declared in this scope

19.03.2014, 16:55. Показов 1465. Ответов 1
Метки нет (Все метки)

пытаюсь скомпилировать такой код:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <mysql/mysql.h>
using namespace std;
 
void exiterr(int exitcode);
 
int main(int argc, char *argv[]) {
     for (int i = 0; i < argc; i++)
         cout << "Argument " << i << " : " << argv[i] << endl;
     mysql_init(&mysql); 
     if(!mysql_real_connect(&mysql, "localhost", "root", "root", "bd", 3306, NULL, 0))
     exiterr(1);
     mysql_close(&mysql);
     return 0;
}
 
void exiterr(int exitcode) {
     // fprintf(stderr, "%s\n", mysql_error(&mysql)); Си вариант
     std::cout << mysql_error(&mysql)<<"\n";
     exit(exitcode);
}
Выводит следующее:
root@example:/home/c# g++ -Wall test.cpp -o test
test.cpp: In function ‘int main(int, char**)’:
test.cpp:10: error: ‘mysql’ was not declared in this scope
test.cpp: In function ‘void exiterr(int)’:
test.cpp:19: error: ‘mysql’ was not declared in this scope
test.cpp:20: error: ‘exit’ was not declared in this scope
Перевел что написано, все равно не понял что ему от меня надо...
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
19.03.2014, 16:55
Ответы с готовыми решениями:

Ошибка cpp|7|error: 'itoa' was not declared in this scope
Почему ошибка? #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(void) { char p; ...

Вот: source_file.cpp: In function 'int main()': source_file.cpp:10:12: error: 'strlen' was not declared in this scope
#include&lt;iostream&gt; using namespace std; int main() { char a,b; int i,j; cout&lt;&lt;&quot;Напишите...

C:\tsserver\Projects\cpp\codeblocks\MyComp\IDE\IDE\IDE.cpp|1 5|error: 'InitApplication' was not declared in this scope|
//=================================================================================================...

Main.cpp: In function ‘int main()’: main.cpp:10:1: error: ‘then’ was not declared in this scope
Написал программу и выдает ошибку. #include &lt;iostream&gt; #include &lt;math.h&gt; using namespace std;...

1
205 / 181 / 112
Регистрация: 15.03.2014
Сообщений: 391
19.03.2014, 17:44 2
1) установить пакет libmysqlclient-dev
2) Файл с исходником называется mysql.c
Внес минимум изменений в твой код.

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <mysql.h>
#include <cstdlib>
 
using namespace std;
 
MYSQL mysql;
 
void exiterr( int exitcode );
 
int main( int argc, char *argv[] ) {
 
    for ( int i = 0; i < argc; i++ )
        cout << "Argument " << i << " : " << argv[ i ] << endl;
 
    mysql_init( &mysql );
    if ( !mysql_real_connect( &mysql, "localhost", "root", "root", "bd", 3306, NULL, 0 ) )
        exiterr( 1 );
    mysql_close( &mysql );
    return 0;
}
 
void exiterr( int exitcode ) {
    // fprintf(stderr, "%s\n", mysql_error(&mysql)); Си вариант
    std::cout << mysql_error( &mysql ) << "\n";
    exit( exitcode );
}
3) Вот команда для компиляции
Код
g++ -I/usr/include/mysql mysql.c -L/usr/mysql -lmysqlclient -o mysql
4) Вот ответ после запуска программы
Код
Argument 0 : ./mysql
Access denied for user 'root'@'localhost' (using password: YES)
0
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
19.03.2014, 17:44
Помогаю со студенческими работами здесь

'arr' was not declared in this scope 'sum' was not declared in this scope такие вот ошибки.(
#include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;ctime&gt; using namespace std; int main () { ...

[Error] 'm' was not declared in this scope
почему программа не видит m,h??? пишет ошибку 'm' was not declared in this scope 'h' was not...

[Error] 'k' was not declared in this scope
#include &lt;iostream&gt; using namespace std; int main() { { int k; char z = { ...

[Error] 'If' was not declared in this scope
Недавно начал изучать Си++. Написал программу почти так же, как в примере из книги. #include...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
2
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2023, CyberForum.ru