Capturing camera list on mac os x does not compile - c++

So I've got a homework at school to list cameras available on mac OS X but I have to do it in C++ under xcode. I created such code:
#include <iostream>
#include <sstream>
#include <string.h>
#include <Quicktime/quicktime.h>
//#include <boost/lexical_cast.hpp>
using namespace std;
int main()
{
int i = 0;
int selectedIndex;
cout << endl << "Let us select video device." << endl << "Available capture devices are:" << endl;
// first get a video channel from the sequence grabber
ComponentDescription theDesc;
Component sgCompID;
ComponentResult result;
theDesc.componentType = SeqGrabComponentType;
theDesc.componentSubType = 0L;
theDesc.componentManufacturer = 'appl';
theDesc.componentFlags = 0L;
theDesc.componentFlagsMask = 0L;
sgCompID = FindNextComponent (NULL, &theDesc);
seqGrabber = OpenComponent (sgCompID);
result = SGInitialize (seqGrabber);
result = SGNewChannel (seqGrabber, VideoMediaType, &videoChannel);
SGDeviceList theDevices;
SGGetChannelDeviceList(videoChannel, sgDeviceListDontCheckAvailability | sgDeviceListIncludeInputs, &theDevices);
if (theDevices)
{
int theDeviceIndex;
for (theDeviceIndex = 0; theDeviceIndex != (*theDevices)->count; ++theDeviceIndex)
{
SGDeviceName theDeviceEntry = (*theDevices)->entry[theDeviceIndex];
cout << i << ".1. " << theDeviceEntry.name << endl;
// name of device is a pstring in theDeviceEntry.name
SGDeviceInputList theInputs = theDeviceEntry.inputs;
if (theInputs != NULL)
{
int theInputIndex;
for ( theInputIndex = 0; theInputIndex != (*theInputs)->count; ++theInputIndex)
{
SGDeviceInputName theInput = (*theInputs)->entry[theInputIndex];
cout << i << ".2. " << theInput.name << endl;
// name of input is a pstring in theInput.name
}
}
}
} // i++ we need to add...
selectedIndex = 999;
if (i <= 0)
{
cout << "No devices found." << endl;
return 999;
}
else if (i == 1)
{
cout << "Default device will be used.\n" << endl;
selectedIndex = 0;
}
else
{
while (selectedIndex > i - 1 || selectedIndex < 0)
{
try
{
cin >> selectedIndex;
//string s;
//getline(cin, s, '\n');
//selectedIndex = boost::lexical_cast<int>(s);
}
catch(std::exception& e)
{
cout << "Please input index from 0 to " << i - 1 << endl;
selectedIndex = 999;
}
}
}
return selectedIndex;
}
It does not compile. It show lots of strange errors about SeqGrabComponentType but I am mac C++ nube and do not know what to do - how to make my app compile please help?
Update:
Errors list:
camerasList: In function 'int main()':
camerasList:49: error: 'SeqGrabComponentType' was not declared in this scope
camerasList:55: error: 'seqGrabber' was not declared in this scope
camerasList:56: error: 'SGInitialize' was not declared in this scope
camerasList:57: error: 'videoChannel' was not declared in this scope
camerasList:57: error: 'SGNewChannel' was not declared in this scope
camerasList:58: error: 'SGDeviceList' was not declared in this scope
camerasList:58: error: expected `;' before 'theDevices'
camerasList:59: error: 'sgDeviceListDontCheckAvailability' was not declared in this scope
camerasList:59: error: 'sgDeviceListIncludeInputs' was not declared in this scope
camerasList:59: error: 'theDevices' was not declared in this scope
camerasList:59: error: 'SGGetChannelDeviceList' was not declared in this scope
camerasList:66: error: 'SGDeviceName' was not declared in this scope
camerasList:66: error: expected `;' before 'theDeviceEntry'
camerasList:67: error: 'theDeviceEntry' was not declared in this scope
camerasList:70: error: 'SGDeviceInputList' was not declared in this scope
camerasList:70: error: expected `;' before 'theInputs'
camerasList:71: error: 'theInputs' was not declared in this scope
camerasList:76: error: 'SGDeviceInputName' was not declared in this scope
camerasList:76: error: expected `;' before 'theInput'
camerasList:77: error: 'theInput' was not declared in this scope
Update:
Half of problem solution: Compiling under i386 architecture solves most errors (few left).

Try to add #import <QuickTime/QuickTimeComponents.h> and link with QuickTime.framework.

As you found, compiling for i386 as opposed to x64 helps a lot.
For the rest, I added
ComponentInstance seqGrabber;
SGChannel videoChannel;
to your code, and it compiled successfully.

Related

Getting compilation errors when trying to use pointers for dynamically allocated 2D array

I've recently started learning coding in the C++ language, and am having issues grasping the concept of pointers in C++.
I'm trying to practice dynamic memory allocation but keep running into errors when trying to compile my program.
Here are some struct objects I've created along with some global variables outside of my main function:
// Storing x and y values from file
struct gridSize {
int gridXValue;
int gridYValue;
string toString();
};
string gridSize::toString ()
{
ostringstream oss;
oss << "gridXValue : " << gridXValue << endl;
oss << "gridYValue : " << gridYValue << endl;
return (oss.str());
}
// Storing details for each grid area within
// 2D array struct
struct grid_area {
int x, y;
bool is_occupied;
int city_id;
string city_name;
int cloud, cloud_index, pressure, pressure_index;
char cloudcover_LMH, pressure_LMH;
};
gridSize gridSize; //Global struct object
grid_area *ga = nullptr; //Global pointer variable
Here is what my main function looks like:
int main ()
{
int userChoice;
// Main menu to run constantly until user enters 8
while (userChoice != 8) {
cout << "Welcome to Weather Information Processing System!" << endl;
cout << "-------------------------------------------------" << endl;
cout << endl;
cout << "1)\tRead in and process a configuration file" << endl;
cout << "2)\tDisplay city map" << endl;
cout << "3)\tDisplay cloud coverage map (cloudiness index)" << endl;
cout << "4)\tDisplay cloud coverage map (LMH symbols)" << endl;
cout << "5)\tDisplay atmospheric pressure map (pressure index)" << endl;
cout << "6)\tDisplay atmospheric pressure map (LMH symbols)" << endl;
cout << "7)\tShow weather forecast summary report" << endl;
cout << "8)\tQuit" << endl;
cout << endl;
cout << "Please enter your choice: ";
cin >> userChoice;
cout << endl;
if (userChoice == 1) {
// Process all files
readAFile();
}
}
// Deallocate memory for 2D array
for (int i = 0; i < gridSize.gridXValue + 1; i++)
{
delete[] ga[i];
}
delete[] ga;
return (0);
}
I highly suspect the issue lies in the last few portions of my readAFile function, but am unable to identify the problem. Here's the portion where I suspect the problem lies in readAFile:
// Allocate array memory
ga = new grid_area [gridSize.gridXValue + 1];
for (int i = 0; i < gridSize.gridYValue + 1; i++)
ga[i] = new grid_area[gridSize.gridYValue + 1];
// Putting x and y values into array
for (int x_array = 0; x_array < gridSize.gridXValue + 1; x_array++) {
for (int y_array = 0; y_array < gridSize.gridYValue + 1; y_array++) {
ga[x_array][y_array].x = x_array;
ga[x_array][y_array].y = y_array;
}
}
This is error I'm getting:
csci251_a1.cpp: In function ‘int main()’:
csci251_a1.cpp:110:20: error: type ‘struct grid_area’ argument given to ‘delete’, expected pointer
delete[] ga[i];
^
csci251_a1.cpp: In function ‘void readAFile()’:
csci251_a1.cpp:172:54: error: no match for ‘operator=’ (operand types are ‘grid_area’ and ‘grid_area*’)
ga[i] = new grid_area[gridSize.gridYValue + 1];
^
csci251_a1.cpp:32:8: note: candidate: ‘grid_area& grid_area::operator=(const grid_area&)’
struct grid_area {
^~~~~~~~~
csci251_a1.cpp:32:8: note: no known conversion for argument 1 from ‘grid_area*’ to ‘const grid_area&’
csci251_a1.cpp:32:8: note: candidate: ‘grid_area& grid_area::operator=(grid_area&&)’
csci251_a1.cpp:32:8: note: no known conversion for argument 1 from ‘grid_area*’ to ‘grid_area&&’
csci251_a1.cpp:177:24: error: no match for ‘operator[]’ (operand types are ‘grid_area’ and ‘int’)
ga[x_array][y_array].x = x_array;
^
csci251_a1.cpp:178:24: error: no match for ‘operator[]’ (operand types are ‘grid_area’ and ‘int’)
ga[x_array][y_array].y = y_array;
I really don't understand what I'm doing wrong here...
If needed, I can provide the rest of the readAFile function as well. Any help would be greatly appreciated.
I should have used a point-to-pointer variable instead of a regular pointer variable for a dynamic 2D array.
Instead of:
grid_area *ga = nullptr;
It should be:
grid_area **ga = nullptr;
And,
ga = new grid_area [gridSize.gridXValue + 1];
Should be:
ga = new grid_area *[gridSize.gridXValue + 1];
Admittedly, I probably should have searched up some more actual examples before hastily posting a question. Thanks to every one who responded!

C++ - Strange problem with loop initialization

The compiler can't handle even the simplest loop
#include <iostream>
using namespace::std;
int main()
{
for( int i = 0, char a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Compiler says this:
prog.cpp: In function ‘int main()’:
prog.cpp:7:18: error: expected unqualified-id before ‘char’
prog.cpp:7:18: error: expected ‘;’ before ‘char’
prog.cpp:7:39: error: expected ‘)’ before ‘;’ token
prog.cpp:7:41: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
prog.cpp:7:41: note: (if you use ‘-fpermissive’ G++ will accept your code)
prog.cpp:7:46: error: ‘a’ was not declared in this scope
prog.cpp:7:50: error: expected ‘;’ before ‘)’ token
And yes, I know I can initialize 'i' and 'a' before the loop. So let's try:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
for(i = 0, char a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Compiler says:
prog.cpp: In function ‘int main()’:
prog.cpp:8:13: error: expected primary-expression before ‘char’
prog.cpp:8:13: error: expected ‘;’ before ‘char’
prog.cpp:8:41: error: ‘a’ was not declared in this scope
When option -std=c++11 used:
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: error: expected unqualified-id before ‘char’
prog.cpp:7:17: error: expected ‘;’ before ‘char’
prog.cpp:7:38: error: expected ‘)’ before ‘;’ token
prog.cpp:7:40: error: ‘i’ was not declared in this scope
prog.cpp:7:45: error: ‘a’ was not declared in this scope
prog.cpp:7:49: error: expected ‘;’ before ‘)’ token
Last one:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
char a = 'A';
for(i = 0, a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Works fine. Guys, am I blind or something? Maybe you need my arch and compiler version:
uname -a
Linux freelancer 3.2.0-4-686-pae #1 SMP Debian 3.2.63-2+deb7u2 i686 GNU/Linux
g++ --version
g++ (Debian 4.7.2-5) 4.7.2
You cannot declare items of different types in the same declaration.
This is true inside and outside of loops. You're not "blind", it's just not valid C++.
Here's the right code:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
char a = 'A';
for(; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Your working version is also valid because the declaration can be swapped out for an expression, though in your case it's redundant because those variables already hold those values at the start of the loop.
26 is so tiny number, thus you can do
#include <iostream>
int main()
{
for( char i = 0, a = 'A'; i <= 26; i++, a++ )
std::cout << "OK, lets try. Showing values: i = "
<< static_cast<int>(i) << ", a = " << a << std::endl;
}
Or even IMHO more clear code, more clear aim, iterating from 'A' until 'Z'.
int main()
{
for( char i = 0, a = 'A'; a <= 'Z'; i++, a++ )
std::cout << "OK, lets try. Showing values: i = "
<< static_cast<int>(i) << ", a = " << a << std::endl;
}

Passing instance of a class to pthread_create

My question is somehow related to this post:
pthread_create error:
I am trying to call a multiple instances of a solver (which is a class perhaps) on different cores. For this I wrote a chuck of code and used pthread_creat() function. Following is my effort:
void *MultiThread(void *threadid)
{
long tid;
tid = (long)threadid;
if(tid==0)
{
cout<< "load thread number "<< tid;
DefaultSolver s1(sys1,prec);
s1.time_limit=time_limit;
s1.trace= true;
sols1=s1.solve(pair2.first);
cout << "number of solutions from solver 1=" << sols1.size() << endl;
cout << "cpu time used=" << s1.time << "s."<< endl;
cout << "number of cells=" << s1.nb_cells << endl;
pthread_exit(NULL);
}
if(tid==1)
{
cout<< "load thread number "<< tid;
DefaultSolver s2(sys2,prec);
s2.time_limit=time_limit;
s2.trace=true;
sols2=s2.solve(pair2.second);
cout << "number of solutions from solver 2=" << sols2.size() << endl;
cout << "cpu time used=" << s2.time << "s."<< endl;
cout << "number of cells=" << s2.nb_cells << endl;
pthread_exit(NULL);
}}
Now DefaultSolver is a class from which I am instantiating 2 instances and declaration should be done on different cores, and then I am using different functions of that class through the declared instances s1 and s2. Following is the code where I am calling this function. Following is the remaining piece of code:
double convert(const char* argname, const char* arg) {
char* endptr;
double val = strtod(arg,&endptr);
if (endptr!=arg+strlen(arg)*sizeof(char)) {
stringstream s;
s << "\"" << argname << "\" must be a real number";
ibex_error(s.str().c_str());
}
return val;
}
int main(int argc, char** argv)
{
pthread_t threads[NUM_THREADS];
int rc;
int i;
try{
// check the number of arguments
if (argc<4) {
ibex_error("usage: defaultsolver filename prec timelimit");
}
vector<IntervalVector> sols1, sols2, sols3, sols4;
System sys1(argv[1]);
System sys2(sys1, System::COPY);
System sys3(sys1, System::COPY);
System sys4(sys1, System::COPY);
double prec = convert("prec", argv[2]);
double time_limit = convert("timelimit",argv[3]);
double bisect = convert("bisect",argv[4]);
pair<IntervalVector,IntervalVector> pair1=sys1.box.bisect(bisect);
pair<IntervalVector,IntervalVector> pair2 = pair1.first.bisect(bisect);
pair<IntervalVector,IntervalVector> pair3= pair1.second.bisect(bisect);
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL,
MultiThread, (void *)i);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);}
And following is the error when I am trying to make it:
parallel2.cpp: In function ‘void* MultiThread(void*)’:
parallel2.cpp:29:26: error: ‘sys1’ was not declared in this scope
parallel2.cpp:29:31: error: ‘prec’ was not declared in this scope
parallel2.cpp:30:17: error: ‘time_limit’ was not declared in this scope
parallel2.cpp:32:3: error: ‘sols1’ was not declared in this scope
parallel2.cpp:32:18: error: ‘pair2’ was not declared in this scope
parallel2.cpp:50:20: error: ‘sys2’ was not declared in this scope
parallel2.cpp:50:25: error: ‘prec’ was not declared in this scope
parallel2.cpp:51:17: error: ‘time_limit’ was not declared in this scope
parallel2.cpp:53:3: error: ‘sols2’ was not declared in this scope
parallel2.cpp:53:18: error: ‘pair2’ was not declared in this scope
parallel2.cpp:64:20: error: ‘sys3’ was not declared in this scope
parallel2.cpp:64:25: error: ‘prec’ was not declared in this scope
parallel2.cpp:65:17: error: ‘time_limit’ was not declared in this scope
parallel2.cpp:67:3: error: ‘sols3’ was not declared in this scope
parallel2.cpp:67:18: error: ‘pair3’ was not declared in this scope
parallel2.cpp:80:20: error: ‘sys4’ was not declared in this scope
parallel2.cpp:80:25: error: ‘prec’ was not declared in this scope
parallel2.cpp:81:17: error: ‘time_limit’ was not declared in this scope
parallel2.cpp:83:3: error: ‘sols4’ was not declared in this scope
parallel2.cpp:83:18: error: ‘pair3’ was not declared in this scope
Is there any way to fix it? or is there any other easy way in c++11?
The variables (sys1, sys2, ... sols1, sols2... prec etc.) are local variables inside your main function, so they are not accessible in your function MultiThread. [Edit: Usually you can only pass one parameter to a thread. If you want to pass multiple parameters you have to create a "container" holding them - a class or a record.]
The ugly solution is to make global variables out of them.
The more common approach is to create a class or record holding all data you need inside your thread, create one instance for each thread and pass that object as the parameter. So you probably even don't neet to know your thread id.
Quick hack:
Create a container class:
class MyThreadParams
{
public:
// you can add getters/setters and hide the member variables if you want
long threadId; //still needed?
System *pSys;
double prec;
double time_limit;
// further values /objects you need
};
In your thread function, cast the parameter to that class and use its values instead of your variables you tried to access from here.
void *MultiThread(void *threadParams)
{
MyThreadParams* pParams = reinterpret_cast<MyThreadParams*>(threadParams);
cout<< "load thread number "<< tid;
DefaultSolver solver(pParams->sys1, pParams->prec);
solver.time_limit = pParams->time_limit;
//and so on
}
The decision which objects shall be used for whicht thread has been done in main when filling MyThreadParams, so you if (threadId == 1) can be ommitted, think.
int main(int argc, char** argv)
{
....
MyThreadParams threadParams[NUM_THREADS];
....
// fill params for 1st thread
threadParams[0].threadId = 0;
threadParams[0].pSys = &sys1;
threadParams[0].prec = convert("prec", argv[2]);
threadParams[0].time_limit := convert("timelimit",argv[3]);
// further values /objects you need
// fill params for 2nd thread
threadParams[1].threadId = 1;
threadParams[2].pSys = &sys2;
threadParams[3].prec = convert("prec", argv[2]);
// and so on
rc = pthread_create(&threads[i], NULL,
MultiThread, reinterpret_cast<void *>(&threadParams[i]));
}
You have to decide whether you want the thread params to be the owner of the System object and the sols etc. or should only carry references/pointer to them and let the exist inside main.

Error Nullptr was not declared

So the nullptr error doesnt show up when i compile it at school and i think i can fix it with adding a line when i compile it, is there another way to get rid of it, and the other two errors i dont understand why im getting them at all. Can someone explain at least the nullptr error?
main.cpp: In function 'int main()':
error: 'array' was not declared in this scope
error: 'hours' was not declared in this scope
error: 'nullptr' was not declared in this scope
int main()
{
float *studentData;
int numStudents;
int size;
int average = getAverage(*array, *hours);
int median = getMedian(hours);
int mode = getMode(hours);
cout << "How many students were surveyed? ";
cin >> numStudents;
studentData = makeArray(numStudents);
if (studentData == nullptr)
cout << "Error allocating memory.";
else
{
getFBData(studentData, numStudents);
selectionSort(studentData, numStudents);
for (int i = 0; i < numStudents; i++)
cout << *(studentData + i) << endl;
delete[] studentData;
}
getAverage(*array, hours);
printArray(size, hours);
getMedian(*array, hours);
getMode(*array, hours);
cout << "STATISTICS " << endl;
cout << "\n Mean: " << average;
cout << "\n Median: " << median;
cout << "\n Mode: " << mode;
return 0;
}
On this line:
int average = getAverage(*array, *hours);
you refer to array and hours, however you have not declared those things yet. The "school version" of the code must have been different.
Re. the nullptr error: that was added to C++ in 2011. Perhaps the school has up-to-date compilers but you have an older compiler at home. If you change nullptr to 0 it will be fine.
The easiest way to solve this problem is to change nullptr to 0. Though not all the time this works. But it can be a small code solution.
You can also use -std=c++11 parameter while compiling using g++.
So the compiling command in the terminal will be :
g++ "your file" -std=c++11

Errors in a sqrt function program in c++

**Essentially I was given pseudo code:
"x = 1
repeat 10 times: x = (x + n / x) / 2
return x"
And the pseudo code for the int main function (int main function to print out my n values in the cout) at the end, in order to create a sqrt function program. I get the following errors on linux2 compiler:
: In function ‘double my_sqrt_1(double)’:
:9:1: error: expected primary-expression before ‘return’
:9:1: error: expected ‘;’ before ‘return’
: In function ‘int main()’:
:
15:13: error: expected unqualified-id before ‘-’ token
:~> expected primary-expression before ‘return’
Help is much appreciated!
#include <iostream>
#include <math.h>
using namespace std;
double my_sqrt_1(double n)
{
for (int x= 1; x<10; ++x)
cout<< x << '\t' << x=(x+n/x)/2 <<
return x;
}
int main()
{
int n= 3.141459;
int k= -100,-10,-1,0,1,10,and 100;
for(auto k : { -100,-10,-1,0,1,10,100}){
n=3.14159 * pow (10.0,k);
cout << "print n,sqrt(n),and my_sqrt_1(n)" ;
return 0;
}
}
You missed a semicolon at the end of the cout line:
double my_sqrt_1(double n)
{
for (int x= 1; x<10; ++x)
cout<< x << '\t' << x=(x+n/x)/2;
return x;
}
The clue is in the error:
:9:1: error: expected ‘;’ before ‘return’
Finding the source of compiler errors can be tricky for those new to C/C++, if you miss a semi-colon the line reported will often differ from the one containing the actual error. As in this case where the return line became part of the same statement as the line above.
Also here:
int k= -100,-10,-1,0,1,10,and 100;
That is not how you define an array, you should read up on the basics of those since you're new to the game, which is evident here:
cout << "print n,sqrt(n),and my_sqrt_1(n)" ;
Where you're not calling any functions but instead outputting a static string of text. You need to make the function calls and variable outputs outside of the literal string:
cout << "print " << n << "," << sqrt(n) << ", and" << my_sqrt_1(n);