‘graphresult’ was not declared in this scope - c++

I'm trying to run a graphics program on my Ubuntu 18.04 LTS system to print the error code for failed graphics operation. My code is
#include <graphics.h>
#include <stdlib.h>
int main()
{
int gd, gm, errorcode;
initgraph(&gd, &gm, NULL);
errorcode = graphresult();
if(errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to exit.");
getch();
exit(1);
}
getch();
closegraph();
return 0;
}
But when I run it I get the following error :
g++ -o mygraphics mygraphics.c -lgraph
mygraphics.c: In function ‘int main()’:
mygraphics.c:10:20: error: ‘graphresult’ was not declared in this scope
errorcode = graphresult();
^~~~~~~~~~~
mygraphics.c:12:24: error: ‘grOk’ was not declared in this scope
if(errorcode != grOk)
^~~~
mygraphics.c:12:24: note: suggested alternative: ‘brk’
if(errorcode != grOk)
^~~~
brk
mygraphics.c:14:42: error: ‘grapherrormsg’ was not declared in this scope
printf("Graphics error: %s\n", grapherrormsg(errorcode));
I searched all over the internet but not able to find a promising solution. Can someone help me out please. Thank you in advance :)

Related

ALSA in C++ - Making the minimal working code

Topic
I would like to make the minimal working code to generate any kind of PCM sound using ALSA in C++ for a Linux computer.
Setup
I'm coding in C++ on Code::Blocks with Ubuntu 20.04.
Background
I used to make simple Arduino UNO programs doing sound processing and just needed to play raw PCM samples.
Issues
ALSA Project's Website is not very easy to understand.
I looked at c - ALSA tutorial required to find out that many links are expired.
I copy pasted the code of the minimal PCM example in C directly into a empty Code::Blocks project and I got that error:
||=== Build: Release in Test (compiler: GNU GCC Compiler) ===|
Test/main.cpp|5|fatal error: ../include/asoundlib.h: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Right in the first line of code which is #include "../include/asoundlib.h".
I'm guessing that the issue could be because I have to download something or add a linker for the compiler.
But I also think it may be an issue of C to C++ conversion meaning that this works in C but not in C++.
Do I have to add a linker for the compiler or download something to make the code working?
Then I looked on ALSA library and downloaded alsa-lib-1.2.3.tar.bz2.
I got an archive that looked to have the right things but I don't know how to handle it.
Then I found usr/include/sound/asound.h on my computer. It looks to be part of ALSA but when I changed the code to use it, it spat out a bunch of errors when used.
The code looks like following now:
/*
* This extra small demo sends a random samples to your speakers.
*/
#include <sound/asound.h>
#include <cstdio>
static char *device = "default"; /* playback device */
unsigned char buffer[16*1024]; /* some random data */
int main(void)
{
int err;
unsigned int i;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
for (i = 0; i < sizeof(buffer); i++)
buffer[i] = random() & 0xff;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
48000,
1,
500000)) < 0) { /* 0.5sec */
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
for (i = 0; i < 16; i++) {
frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
break;
}
if (frames > 0 && frames < (long)sizeof(buffer))
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
}
/* pass the remaining samples, otherwise they're dropped in close */
err = snd_pcm_drain(handle);
if (err < 0)
printf("snd_pcm_drain failed: %s\n", snd_strerror(err));
snd_pcm_close(handle);
return 0;
}
And the errors are like that:
||=== Build: Release in Test (compiler: GNU GCC Compiler) ===|
Test/main.cpp|6|warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]|
Test/main.cpp||In function ‘int main()’:|
Test/main.cpp|12|error: ‘snd_pcm_t’ was not declared in this scope; did you mean ‘snd_pcm_info’?|
Test/main.cpp|12|error: ‘handle’ was not declared in this scope|
Test/main.cpp|16|error: ‘SND_PCM_STREAM_PLAYBACK’ was not declared in this scope; did you mean ‘SNDRV_PCM_STREAM_PLAYBACK’?|
Test/main.cpp|16|error: ‘snd_pcm_open’ was not declared in this scope; did you mean ‘snd_pcm_info’?|
Test/main.cpp|17|error: ‘snd_strerror’ was not declared in this scope|
||error: %s\n", snd_strerror(err));|
Test/main.cpp|21|error: ‘SND_PCM_FORMAT_U8’ was not declared in this scope; did you mean ‘SNDRV_PCM_FORMAT_U8’?|
Test/main.cpp|22|error: ‘SND_PCM_ACCESS_RW_INTERLEAVED’ was not declared in this scope; did you mean ‘SNDRV_PCM_ACCESS_RW_INTERLEAVED’?|
Test/main.cpp|20|error: ‘snd_pcm_set_params’ was not declared in this scope; did you mean ‘snd_pcm_sw_params’?|
Test/main.cpp|27|error: ‘snd_strerror’ was not declared in this scope|
||error: %s\n", snd_strerror(err));|
Test/main.cpp|31|error: ‘snd_pcm_writei’ was not declared in this scope|
Test/main.cpp|33|error: ‘snd_pcm_recover’ was not declared in this scope|
Test/main.cpp|35|error: ‘snd_strerror’ was not declared in this scope|
Test/main.cpp|42|error: ‘snd_pcm_drain’ was not declared in this scope|
Test/main.cpp|44|error: ‘snd_strerror’ was not declared in this scope|
Test/main.cpp|45|error: ‘snd_pcm_close’ was not declared in this scope|
||=== Build failed: 17 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
Follow these steps:
Install the ALSA development package, or make sure it is already installed. The name depends on your distribution. In my case (OpenSUSE Tumbleweed) it is alsa-devel. This will install the file asoundlib.h under the directory /usr/include/alsa.
Change the line in your code from #include "../include/asoundlib.h" to #include <alsa/asoundlib.h>. Notice the angular brackets instead of quotation marks.
The library which you want to link against is named libsound.so, so compile the program with a command like gcc -Wall pcm_min.c -lasound -o pcm_min
Run the program: ./pcm_min

error: invalid conversion from ‘int’ to ‘void*’ [-fpermissive] in the given set of commands

I have c++ file like below one,
#include <iostream>
using namespace std;
extern "C" {
#include "sample_publish.c"
}
int main()
{
int antenna_id = 123;
send_message_to_mqtt(&antenna_id);
}
I have included a c file in c++ file and I need to pass the variable antenna_id to the function send_message_to_mqtt and the same is in c file like below one.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
void send_message_to_mqtt(int *antenna_id) {
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
MQTTClient_create(&client, "tcp://mqtt1.mindlogic.com:1883", "ExampleClientPub",
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("DATA FROM C++:::%d\n", *antenna_id);
char payload_data[] = "hi";
//pubmsg.payload = payload_data;
pubmsg.payload = *antenna_id
pubmsg.payloadlen = (int)strlen(*antenna_id);
pubmsg.qos = 1;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, "MQTT-Examples", &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s\n""on topic %s for client with ClientID: %s\n",(int)(10000L/1000), "Hello World!", "MQTT-Examples", "ExampleClientPub");
rc = MQTTClient_waitForCompletion(client, token, 10000L);
printf("Message with delivery token %d delivered\n", token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
// return rc;
}
When I execute a c++ file, the antenna_id variable is doesnt accessible in c file which in turn not allowing me to map against pubmsg.payload and this is due to the below error,
dell#dell-Inspiron-5379:~/workspace_folder$ g++ sample.cpp -o sample -lpaho-mqtt3c
In file included from sample.cpp:5:0:
sample_publish.c: In function ‘void send_message_to_mqtt(int*)’:
sample_publish.c:30:22: error: invalid conversion from ‘int’ to ‘void*’ [-fpermissive]
pubmsg.payload = *antenna_id
^~~~~~~~~~~
sample_publish.c:31:5: error: expected ‘;’ before ‘pubmsg’
pubmsg.payloadlen = (int)strlen(*antenna_id);
^~~~~~
How to overcome this one?
A guess on the problem, it's most likely this line:
pubmsg.payload = *antenna_id
Besides missing the semicolon, the payload is a pointer to the first byte of the data to be sent. That is, you should not dereference the pointer:
pubmsg.payload = antenna_id;
On a related note, this line is also very wrong:
pubmsg.payloadlen = (int)strlen(*antenna_id);
The strlen function is to get the length if a null-terminate byte string.
The length of an int can be gotten with the sizeof operator:
pubmsg.payloadlen = sizeof *antenna_id;
Note that here you must use the dereference operator, otherwise you get the size of the pointer itself.

C++ Compile error: expected ‘,’ or ‘...’ before ‘(’ token [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Very specific C++ compilation syntax error that I can't figure out a solution to: I'm fairly new to C++ and read up on functions being passed as parameters. The compile error doesn't make sense to me because I've read the code over-and-over. Please help.
Edit: I removed the waitpid(pid_t,int,int) and stuck with just system() commands. Thanks for the help everyone.
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
/* puts apt-get purge text into ~$ purge_e_output */
system("sudo apt-get purge enlightenment > purge_e_output.txt");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
system("echo **REMOVING ENGLIGHTENMENT**");
system("sudo apt-get purge enlightenment");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* puts apt-get autoremove text into ~$ autoremove_e_output */
system("sudo apt-get autoremove > autoremove_e_output.txt");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
system("echo **REMOVING E DATA**");
system("sudo apt-get autoremove");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* puts apt-get autoclean text into ~$ autoclean_e_output */
system("sudo apt-get autoclean > autoclean_e_output.txt");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
system("echo **CLEANING**");
system("sudo apt-get autoclean");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* reinstall englightenment */
system("echo **REINSTALLING**");
system("sudo apt-get install enlightenment");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* logs */
system("echo && echo Logs for wtf just happened:");
system("echo && echo ~$ purge_e_output.txt && echo ~$ autoremove_e_output.txt && echo ~$ autoclean_e_output.txt");
}
Compile error:
~$ g++ JIC.cpp -o JIC
JIC.cpp: In function ‘int main()’:
JIC.cpp:12: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:18: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:26: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:34: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:42: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:49: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:58: error: expected ‘,’ or ‘...’ before ‘(’ token
This doesn't make sense in that place:
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
You cannot do this
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
instead you need to actually create instances of the variables that you want to pass to this function. For example
pid_t pid = getpid();
int status = // set to some value that you choose
then you can call your function like this
pid_t pid2 = waitpid(pid, &status, WUNTRACED);
now pid2 is the value returned by the waitpid function.
I would strongly advise you to do a little more reading - whether it be books or tutorials - as your question shows you don't yet have a strong grasp of the language. I mean absolutely no offence by that at all - just trying to help.
The line
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
looks like a function declaration, both to me and to the C++ compiler. It looks like you're trying to declare a function named waitpid whose return type is pid_t, which takes three parameters, a pid_t, an int*, and an int. However, the name of a parameter can't be getpid("apt-get"), so besides this being the wrong place in the file for a function declaration, you can't interpret this as a function declaration because it tries to use a function call as the name of a parameter.
Conversely, if you try to interpret this line as a function call to a function named waitpid (that's already defined), getpid("apt-get") makes sense as a parameter because it means you want to pass the result of the getpid function as the first parameter to the waitpid function. However, the rest of that line is incorrect syntax for a function call, because the types of the parameters to a function call should not be specified inline - you should be passing it parameters that have already been defined as variables or functions. A syntactically-correct call to the waitpid function might look like this:
int* statusPtr = ...;
int WUNTRACED = ...;
pid_t myPid = waitpid(getpid("apt-get"), &statusPtr, WUNTRACED);
And the following definition might appear elsewhere:
pid_t waitpid(pid_t pid, int *statusPtr, int WUNTRACED) {
...
}

Failling Qt application on Ubuntu

I have Ubuntu 10.04 and have the Qt library install. When I run the code
#include <QDir>
#include <QFileInfo>
#include <QtDebug>
int main( int argc, char **argv )
{
foreach( QFileInfo drive, QDir::drives() )
{
qDebug() << "Drive: " << drive.absolutePath();
QDir dir = drive.dir();
dir.setFilter( QDir::Dirs );
foreach( QFileInfo rootDirs, dir.entryInfoList() )
qDebug() << " " << rootDirs.fileName();
}
return 0;
}
I get the following errors.
g++ qt.cpp -o test
qt.cpp:1:16: error: QDir: No such file or directory
qt.cpp:2:21: error: QFileInfo: No such file or directory
qt.cpp:4:19: error: QtDebug: No such file or directory
qt.cpp: In function ‘int main(int, char**)’:
qt.cpp:8: error: ‘QFileInfo’ was not declared in this scope
qt.cpp:8: error: ‘QDir’ has not been declared
qt.cpp:8: error: ‘foreach’ was not declared in this scope
qt.cpp:9: error: expected ‘;’ before ‘{’ token
How do I fix this problem?
g++ seems to not find Qt includes files.
You should add an include directory when compiling. and linked with the Qt library.

Why does this compile in C but not C++ (sigaction)?

I get the following errors when trying to compile the below code using g++. When I compile it using gcc it works fine (other than a few warnings). Any help appreciated.
g++ ush7.cpp
ush7.cpp: In function ‘int signalsetup(sigaction*, sigset_t*, void (*)(int))’:
ush7.cpp:93: error: expected unqualified-id before ‘catch’
ush7.cpp:95: error: expected primary-expression before ‘catch’
ush7.cpp:95: error: expected `;' before ‘catch’
ush7.cpp:97: error: expected primary-expression before ‘catch’
ush7.cpp:97: error: expected `;' before ‘catch’
ush7.cpp:100: error: expected primary-expression before ‘catch’
ush7.cpp:100: error: expected `)' before ‘catch’
ush7.cpp:108: error: expected `)' before ‘;’ token
ush7.cpp:108: error: expected `)' before ‘;’ token
ush7.cpp: In function ‘int makeargv(const char*, const char*, char***)’:
ush7.cpp:137: error: invalid conversion from ‘void*’ to ‘char*’
ush7.cpp:145: error: invalid conversion from ‘void*’ to ‘char**’
int signalsetup(struct sigaction *def, sigset_t *mask, void (*handler)(int))
{
struct sigaction catch;
catch.sa_handler = handler; /* Set up signal structures */
def->sa_handler = SIG_DFL;
catch.sa_flags = 0;
def->sa_flags = 0;
if ((sigemptyset(&(def->sa_mask)) == -1) ||
(sigemptyset(&(catch.sa_mask)) == -1) ||
(sigaddset(&(catch.sa_mask), SIGINT) == -1) ||
(sigaddset(&(catch.sa_mask), SIGQUIT) == -1) ||
(sigaction(SIGINT, &catch, NULL) == -1) ||
(sigaction(SIGQUIT, &catch, NULL) == -1) ||
(sigemptyset(mask) == -1) ||
(sigaddset(mask, SIGINT) == -1) ||
(sigaddset(mask, SIGQUIT) == -1))
return -1;
return 0;
}
catch is a keyword in C++ but not in C.
Please see my related answer C is not a proper subset of C++ here, or even better here.
You should be able to use a #define if you don't want to change the rest of the code.
#define catch _catch