ERROR: slist was not declared in this scope - c++

This code isn't working for me, I am getting below-mentioned compilation errors.
Somebody please help me to point out my mistake.
In function 'int main()':
4:3: error: 'slist' was not declared in this scope
4:9: error: expected primary-expression before 'int'
5:3: error: 'L' was not declared in this scope
12:9: error: expected primary-expression before 'int'
13:3: error: 'back' was not declared in this scope
Code:
int main() {
slist<int> L;
L.push_front(0);
L.push_front(1);
L.insert_after(L.begin(), 2);
copy(L.begin(), L.end(), // The output is 1 2 0
ostream_iterator<int>(cout, " "));
cout << endl;
slist<int>::iterator back = L.previous(L.end());
back = L.insert_after(back, 3);
back = L.insert_after(back, 4);
back = L.insert_after(back, 5);
copy(L.begin(), L.end(), // The output is 1 2 0 3 4 5
ostream_iterator<int>(cout, " "));
cout << endl;
}

All the problems boil down to the fact that the following declaration
slist<int> L;
throwed the error that
slist was not declared in this scope
You need to make sure slist is declared in your scope. include the necessary headers.

Related

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;
}

I'm trying to slice my c++ array from the second to the last index

I'm trying to emulate the echo command in C++.
I'm trying to slice the the program name off of the entry values and push the
rest to the command line. BUT I'm getting weird errors.
Here's my code:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
if(argv[1] == "echo"){
cout << args[2:];
}
return 0;
}
But I get the error(s):
cmd.cpp: In function 'int main(int, const char**)':
cmd.cpp:6:13: error: 'args' was not declared in this scope
cout << args[:];
^~~~
cmd.cpp:6:13: note: suggested alternative: 'argc'
cout << args[:];
^~~~
argc
cmd.cpp:6:18: error: expected primary-expression before ':' token
cout << args[:];
^
cmd.cpp:6:18: error: expected ']' before ':' token
cout << args[:];
^
]
I am trying to take {1234545, "hello", ", world!"} and turn It into "hello, world!" Basically what I want to do is get rid of array[0] and joining the rest of the list together.
EDIT: Thanks #chipster for giving a great answer!
Minor issue (I mean, I guess it's a big one, as it's the one causing the compiler error, but once you fix it, you're going to get rammed with another error really quick, so...): args does not exist. You actually want argv instead.
The syntax arr[i:j] it Python syntax, not C++.
To do the equivalent in C++, do this instead:
for(int i=2;i<argc;i++) {
std::cout << argv[i] << "\n"; // "\n" is just to make things look nicer.
// "\n" could be any separator
}

expected initializer before ‘:’ token

My code looks likes below:
#include <vector>
#include <iostream>
#include <map>
using namespace std;
int main()
{
std::map<int, std::vector<int> > map;
map[1].push_back(5);
map[1].push_back(3);
map[3].push_back(2);
map[2].push_back(1);
map[1].push_back(-1);
map[3].push_back(2);
int sum2 = 0;
for (const pair<int, vector<int> >& index_vec : map)
{
int sum = 0;
for (int elem : index_vec.second)
{
sum += elem;
}
sum2 += sum*sum;
cout << "index " << index_vec.first << ": " << sum << endl;
}
cout << "sum_2: " << sum2 << endl;
return 0;
};
Which works fine in my laptop but when using desktop gives me following erors:
map.cpp: In function ‘int main()’:
map.cpp:17: error: expected initializer before ‘:’ token
map.cpp:29: error: expected primary-expression at end of input
map.cpp:29: error: expected ‘;’ at end of input
map.cpp:29: error: expected primary-expression at end of input
map.cpp:29: error: expected ‘)’ at end of input
map.cpp:29: error: expected statement at end of input
map.cpp:29: error: expected ‘}’ at end of input
The output should be:
index 1: 7
index 2: 1
index 3: 4
sum_2: 66
which my laptop gives as expected. I have absolutely no idea, could soeone please help me out?
Your code works on me without any modification. Please check your compiler. You will need a C++11 compiler. If you use g++, something like this:
g++ -std=c++11 A.cpp
https://gcc.gnu.org/projects/cxx-status.html#cxx11. You need at least 4.7. Please update your ancient compiler.

Why am I getting the error "cin does not name a type"

While trying to write this code, I get an error "cin doesnt name a type".
I don't know what the problem is exactly and I tried to write "using namespace std;"
but it gave the same error.
Here's the code
#include<iostream>
namespace myStuff {
int value = 0;
}
using namespace myStuff;
int main {
std::cout << "enter integer " << ;
std::cin >> value;
std::cout << "\nyouhaveenterd a value" << value ;
return 0;
}
Here's the compilation error :
: extended initializer lists only available with `-std=c++0x` or `-std=gnu++0x` [enabled by default]|
: expected primary-expression before ‘;’ token|
expected `}` before `;` token|
`cin` does not name a type|
: `cout` does not name a type|
: expected unqualified-id before `return`|
: expected declaration before `}` token|
||=== Build finished: 6 errors, 1 warnings ===|
int main{
should be
int main(){
and
std::cout << "enter integer " << ;
should be
std::cout << "enter integer ";
On this line:
std::cout << "enter integer " << ;
There's no corresponding operand to make the statement syntactically valid. That's probably the source of your errors.
Its the previous line.
cout<<"enter integer" **<<** ;
that last << is expecting an argument which is never given

Capturing camera list on mac os x does not compile

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.