Code Blocks C++ Error expected primary expression before enum - c++

I am self teaching myself in C++ so I just would like to ask for your forgiveness if my question is really basic.
I am following a tutorial on www.learncpp.com
According to the tutorial, I could define my c++ array such as like this
int main()
{
using namespace std;
enum ArrayElements
{
MAX_ARRAY_SIZE = 5;
};
int anArray[MAX_ARRAY_SIZE];
return 0;
}
But codeblock keep on issuing error
||=== Build: Debug in CH6 (compiler: GNU GCC Compiler) ===|
In function 'int main()':|
|6|error: expected primary-expression before 'enum'|
error: expected ';' before 'enum'|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I just dont know what is causing the error or is there a problem with the tutorial I am following?

Remove the semicolon inside the enum.
MAX_ARRAY_SIZE = 5;
// ^
If you do have more names inside the enum, separate them with a comma ,
enum COLOR
{
RED,
BLUE,
GREEN
};

replace enum ArrayElements block with following code
int MAX_ARRAY_SIZE = 5;

Related

Constructing a class in a template class

I am studying about templates and typename keyword I am getting error in the following code:
/*1)*/ #include<iostream>
/*2)*/ #include<cstdio>
/*3)*/ #include<stdlib.h>
/*4)*/
/*5)*/ using namespace std;
/*6)*/
/*7)*/ class out
/*8)*/ {
/*9)*/ public:
/*10)*/ int i;
/*11)*/ out(int i,int j):i{i},ob{j}{}
/*12)*/ class in
/*13)*/ {
/*14)*/ public:
/*15)*/ int j;
/*16)*/ in(int j):j{j}{}
/*17)*/ }ob;
/*18)*/ };
/*19)*/
/*20)*/ template<typename type>
/*21)*/ class temp
/*22)*/ {
/*23)*/ public:
/*24)*/ typename type::in ob(3);
/*25)*/ type ob1(4,4);
/*26)*/ };
/*27)*/
/*28)*/ int main()
/*29)*/ {
/*30)*/ out ob(1,1);
/*31)*/ out::in ob1(2);
/*32)*/ temp<out> t;
/*33)*/ cout<<ob.i<<" "<<ob.ob.j<<endl;
/*34)*/ cout<<ob1.j<<endl;
/*35)*/ cout<<t.ob.j<<endl;
/*36)*/ cout<<t.ob1.i<<" "<<t.ob1.ob.j;
/*37)*/ }
The code shows the following error
Line Error
|24| error: expected identifier before numeric constant
|24| error: expected ',' or '...' before numeric constant
|25| error: expected identifier before numeric constant
|25| error: expected ',' or '...' before numeric constant
In function 'int main()':
|35| error: 't.temp<type>::ob<out>' does not have class type
|36| error: 't.temp<type>::ob1<out>' does not have class type
|36| error: 't.temp<type>::ob1<out>' does not have class type
=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 4 second(s)) ===
If i change the two lines
typename type::in ob(3);
type ob1(4,4);
To
typename type::in ob=typename type::in(3);
type ob1=type(4,4);
It will works fine and producing following output:
1 1
2
3
4 4
Process returned 0 (0x0) execution time : 0.847 s
Press any key to continue.
But i want to know why the error shows, How can i solve the error in above code Please help me?
Thanks for helping.
If you want to initialize variables in the definition of a class you have to use assignement syntax or curly braces. Plain paranthesis is not allowed.
typename type::in ob=typename type::in(3);
type ob1=type(4,4);
typename type::in ob{3};
type ob1{4,4};
This is unrelated to templates and works the same for all classes. One of the reasons is to make parsing easier for the compiler. As mentioned in the comments most vexing parse is an example when disambiguating between an initialization and a function declaration can be done by using {} instead of ().

why atomic does not work with auto varaible

I am not sure what is wrong with following statements , its giving me compilation errors. Can we not use "auto" with atomic variables?
#include <iostream>
#include<future>
#include <atomic>
using namespace std;
int main()
{
atomic<int> value(10);
auto NewValue = value;
}
but if I replace "auto" with "int" , it works. Why?
int main()
{
atomic<int> value(10);
int NewValue = value;
}
Compilation error with "auto"
||=== Build: Debug in Hello (compiler: GNU GCC Compiler) ===|
F:\3d\C++CodeProject\Hello\main.cpp||In function 'int main()':|
F:\3d\C++CodeProject\Hello\main.cpp|11|error: use of deleted function
'std::atomic<int>::atomic(const std::atomic<int>&)'|
C:\Program Files
(x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\atomic|612|note:
declared here|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
auto matches the data type on the right-hand side of the assignment. In this statement:
auto NewValue = value;
value is a std::atomic<int>, so auto will deduce to std::atomic<int>, not to int like you are expecting.
IOW, this:
auto NewValue = value;
Is the same as this:
atomic<int> NewValue = value;
Which is copy initialization using a copy constructor, but std::atomic has a delete'd copy constructor, which is exactly what the error message says:
use of deleted function 'std::atomic<int>::atomic(const std::atomic<int>&)'
std::atomic has a conversion operator for T, which is why int NewValue = value; works.
Atomic variables are not copy constructible: http://en.cppreference.com/w/cpp/atomic/atomic/atomic (3)
This is what auto will attempt to do in this instance.
However, you can cast it down to an int, using the operator int conversion: http://en.cppreference.com/w/cpp/atomic/atomic/operator_T

Prototyped function isn't defined? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
So I've got a project file in CodeBlocks for Project Euler, but I'm a bit confused about what I've done wrong in setting my code up. I have a main.cpp file for running my programs, and I prototype each problem's function before I use it in the main block. However, I have this error when trying to build it:
||=== Build: Debug in Project Euler (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function main':|
C:\Users\under\cpp-workspace\Project Euler\main.cpp|9|undefined reference top4()'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
This is what I have, so I'm not sure what's wrong?
main.cpp:
#include <iostream>
using namespace std;
void p4();
int main()
{
p4();
return 0;
}
p4.cpp:
#include <iostream>
using namespace std;
void p4()
{
cout << "hello there" << endl;
}
I'm not sure what's wrong?
My question is not a duplicate, at least not that I can tell. The question this is supposedly a duplicate of never mentions the issue I'm having.
I've checked, and p4() is a void function with no inputs, so I'm not sure what's wrong.
You have declared p4() but you haven't defined it. Add
void p4()
{
}
to your file for an empty definition. If you need to do more in p4, add whatever code you want to.
Update
You just need to add p4.cpp to the set of files in your project.

C++ array error about does not match a type [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is my code and I get the following error.
Code
#include <vector>
#include <iostream>
//...
using namespace std;
main(){
vector<int> arrayi;
int i = 999; // some integer value
arrayi.reserve(10); // make room for 10 elements
arrayi.push_back(i);
cout<<arrayi.capacity()<<endl;
cout<<arrayi.size()<<endl;
}
Error
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
error: 'arrayi' does not name a type|
error: 'arrayi' does not name a type|
error: 'cout' does not name a type|
error: 'cout' does not name a type|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
where did I go wrong??
You may not have expression statements in the namespace-/file-scope. Only declaration statements are allowed.
Declare a function, and write the expressions in the block scope of that function. In particular, I suggest declaring the main function, because a C++ program must contain one. Main function is the entry point of the program.
Your code is in global namespace, it should go in a function like main().
#include <vector>
#include <iostream>
int main()
{
std::vector<int> arrayi;
int i = 999; // some integer value
arrayi.reserve(10); // make room for 10 elements
arrayi.push_back(i);
std::cout << arrayi.capacity() << std::endl;
std::cout << arrayi.size() << std::endl;
}
Demo

Array of Pointers assigning value

Works:
int GlobalVar = 5;
int * LPVar[] = {&GlobalVar};
Doesn't work:
int GlobalVar = 5;
int * LPVar[]; // int * LPVar[1] doesn't work too
LPVar[0] = &GlobalVar; // errors here
------ Build started: Project: pointers, Configuration: Release Win32 ------ Compiling... ilovpointers.cpp .\ilovpointers.cpp(9) : error C2466: cannot allocate an array of constant size 0
.\ilovpointers.cpp(9) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int .\ilovpointers.cpp(9)
: error C2040: 'LPVar' : 'int []' differs in levels of indirection
from 'int *[1]' .\ilovpointers.cpp(9) : error C2440: 'initializing' :
cannot convert from 'int *' to 'int []'
There are no conversions to array types, although there are conversions to references or pointers to arrays Build log was saved at
"file://f:\Visual Studio
C++\Project1\pointers\pointers\Release\BuildLog.htm" pointers - 4
error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
EDIT:
I solved but got question.
#include <iostream>
#include <stdio.h>
using namespace std;
int GlobalVar = 5;
int * LPVar[1];
void main()
{
LPVar[0] = &GlobalVar;
printf("%i", *LPVar[0]);
cin.get();
}
LPVar is a global array so why it didn't work when I have put it just after definition?
Is the code at file scope?
The answer appears to be yes given the updated code fragment.
If so, you can't have random assignments like LPVar[0] = &GlobalVar; written at file scope. You can only have declarations (without initializers) or definitions (optionally with initializers) at file scope.
In your question, this works (as indeed it should), because there are two variable definitions with initializers.
int GlobalVar = 5;
int *LPVar[] = {&GlobalVar};
This code does not work (as indeed it shouldn't), because the third line is an assignment statement and not a declaration or definition:
int GlobalVar = 5;
int *LPVar[]; // Declaration, not definition
LPVar[0] = &GlobalVar; // Assignment is not allowed outside a function body
Variant:
int GlobalVar = 5;
int *LPVar[1]; // Definition without initializer
LPVar[0] = &GlobalVar; // Assignment is not allowed outside a function body