static int array in a class problem - c++

The following 3 code blocks are the main.cpp, static_class_array.cpp, and static_class_array.h respectively. I'm getting the following error:
static_class_array.cpp||In constructor 'static_array_class::static_array_class()':|
static_class_array.cpp|5|error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment|
||=== Build finished: 1 errors, 0 warnings ===|
#include "static_class_array.h"
int main()
{
static_array_class* array_class;
array_class = new static_array_class();
delete array_class;
return 0;
}
#include "static_class_array.h"
static_array_class::static_array_class()
{
static_array_class::array[3] = {0,1,2};
}
static_array_class::~static_array_class(){}
#ifndef STATIC_CLASS_ARRAY_H
#define STATIC_CLASS_ARRAY_H
class static_array_class
{
private:
static int array[3];
public:
static_array_class();
~static_array_class();
};
#endif

I think that what you want in the implementation file is:
static_array_class::static_array_class()
{
}
static_array_class::~static_array_class(){}
int static_array_class::array[3] = {0,1,2};
Explanation of error message
"cannot convert 'brace-enclosed initializer list' to 'int' in
assignment"
in submitted code.
This is because the code:
static_array_class::array[3] = {0,1,2};
is interpreted as meaning that {0,1,2} should be assigned to element 3 in the array. Element 3 is of type int, (and incidentally not allocated being the fourth element), so this is like:
int i = 0;
i = {0,1,2};
Hence the error message.

They are not the same type;
Your class is a class which includes a an array -- they other is just an array.
With a static definition of a class member you need to declare the actual instance outside the class, just like with any other static,
int static_array_class::array[3] = {0,1,2}; // note this line is outside the constructor
static_array_class::static_array_class()
{
}
static_array_class::~static_array_class(){}

Related

Error making tuple containing a unique_ptr

I am trying to create a function func that returns an std::tuple of an std::unique_ptr<A> and a double. However, when I try to create the tuple I receive errors. The code follows:
#include <tuple>
#include <memory>
class A {
public:
A() : data (3){}
private:
double data;
};
std::tuple<std::unique_ptr<A>, double> func(double num) {
std::unique_ptr<A> a = std::make_unique<A>();
std::tuple<std::unique_ptr<A>, double> temp = std::make_tuple(a, num); // ERROR MESSAGE C
return temp;
}
int main() {
return 0;
}
This code produces the following 4 errors in Visual Studio.
Error message A:
tuple(827,18): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to '_Ttype'
Error message B:
tuple(825,83): message : No constructor could take the source type, or constructor overload resolution was ambiguous
Error message C:
Source.cpp(14): message : see reference to function template instantiation 'std::tuple<std::unique_ptr<A,std::default_delete<A>>,double> std::make_tuple<std::unique_ptr<A,std::default_delete<A>>&,double&>(std::unique_ptr<A,std::default_delete<A>> &,double &)' being compiled
Error message D:
tuple(827,12): error C2064: term does not evaluate to a function taking 2 arguments
First, what is the cause of this error? Second, is the A associated with a being placed on the heap?
A std::unique_ptr is not copyable. You are attempting to copy a std::unique_ptr to the tuple, and that will not work. The compiler error message is kind of cryptic, IMO, but that is basically what seems to be the problem.
However, a std::unique_ptr is moveable, thus you can use std::move:
std::tuple<std::unique_ptr<A>, double> temp = std::make_tuple(std::move(a), num);

Can we send pair a template argumrent another pair

Can we use pair as an argument for another pair in C++.
Here is a test program to do that.
#include <iostream>
#include <utility>
int main()
{
std::pair<int,int> m;
m=std::make_pair(1,3);
int r = 3;
int *q = &r;
int **p =&q;
int **t = p;
std::pair<int**,<std::pair<int,int> > > i(p,m);
std::cout<<i[t];
return 0;
}
This is the error, I am getting.
Test.cpp: In function ‘int main()’:
Test.cpp:12:45: error: template argument 2 is invalid
std::pair<int**,<std::pair<int,int>>> i(p,m);
^
If I changed the format of declaration and wrote the program in the following way,
#include <iostream>
#include <utility>
int main()
{
std::pair<int,int> m;
m=std::make_pair(1,3);
int r = 3;
int *q = &r;
int **p =&q;
int **t = p;
std::pair<
int**,
<
std::pair<
int,
int
>
>
> i(p,m);
std::cout<<i[t];
return 0;
}
There is an additional error.
Test.cpp: In function ‘int main()’:
Test.cpp:20:7: error: template argument 2 is invalid
> i(p,m);
^
Test.cpp:20:14: error: expression list treated as compound expression in initializer [-fpermissive]
> i(p,m);
What might be the issue and how to solve it?
On a side note, I did a program and compiled it in an very old Dev-c++ compiler on a windows 7 machine which used a code similar to the above and it ran perfectly fine. It was a lab program in my college which had only that compiler.
The above code, I ran on both windows 7 and Ubuntu 16.04 using GNU g++ 5.4.0
Ideone: Link to the actual program for reference
You might also have a look at the actual error, I faced in the above link.
This is incorrect:
std::pair<int**,<std::pair<int,int> > > i(p,m);
Just remove the extra <>:
std::pair<int**, std::pair<int,int > > i(p,m);
Also, I'm not sure what you're trying to do in the cout part, but pair doesn't have a [] operator. You can access the elements with first and second:
i.first
i.second
i.second.first
i.second.second
You may also be interested in std::tuple if you don't want to nest one pair into another:
std::tuple<int**, int, int> i;
There are two problems with your code.
You have a superfluous pair of angle brackets in your template instantiation.
The cout parameter makes no sense
#include <iostream>
#include <utility>
int main()
{
std::pair<int,int> m;
m=std::make_pair(1,3);
int r = 3;
int *q = &r;
int **p =&q;
int **t = p;
std::pair<
int**,
// < Omit this
std::pair<
int,
int
>
// > also this
> i(p,m);
// std::cout<<i.[t]; indexing with a pointer value doesn't make sense
// i. for a pair doesn't make sense, specify either first or second
std::cout<<i.first[0]; // <<<< Did you mean something like this?
return 0;
}
Check the compiling version live

How to use a default argument for std::array? "array must be initialized with a brace-enclosed initializer"

My constructor takes an std::array. I'm trying to give it a default value, but calling the constructor without an argument gives this error:
$ g++ -std=c++11 -Wall -Werror -Wextra -pedantic-errors test.cpp Position.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:14:29: error: array must be initialized with a brace-enclosed initializer
Position *y = new Position();
^
test.cpp:14:29: error: too many initializers for ‘std::array<unsigned char, 8ul>’
Line numbers are different. This is my code:
// test.cpp
int main() {
Position *x = new Position({1,1,1,1,1,1,1,1}); // works
Position *y = new Position(); // does not work
}
// Position.cpp
#include <cstdint>
#include <array>
#include "Position.h"
Position::Position( std::array<uint8_t,8> columns_ ) {
columns = columns_;
}
// Position.h
#ifndef POSITION_H
#define POSITION_H
#include <array>
class Position {
public:
Position( std::array<uint8_t,8> = {0,0,0,0,0,0,0,0} );
private:
std::array<uint8_t,8> columns;
};
#endif
What have I got wrong?
You need to repeat the type:
Position( std::array<uint8_t,8> = std::array<uint8_t,8>{0,0,0,0,0,0,0,0} );
while this (or using double-braces) works, it creates a copy on each caller's side when the default is taken. An overloaded constructor would avoid this kind of code-bloat, hence I'd advice to prefer overloads over default parameters.
double-braces required for the second case to run:
Position( std::array<uint8_t,8> = {{0,0,0,0,0,0,0,0}} );

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

Pointer to member, within class

I am trying create a typedef for a template that needs a pointer to member, from within a class. The best way to describe it is to show the minimal sample code:
template<typename T, int T::*MV>
struct Bar
{
const int &value(const T &t) const
{
return (t.*MV);
}
};
struct Foo
{
int baz;
typedef Bar<Foo, &Foo::baz> GetBaz; // Compiler error
};
typedef Bar<Foo, &Foo::baz> GetFooBaz; // Compiles just fine
int main(int argc, char* argv[])
{
Foo f = { 42 };
Foo::GetBaz b; // Fails to compile
GetFooBaz b2; // Fine
int val = b.value(f); // Fails to compile because of above
int val2 = b2.value(f); // Fine
}
I am not necessarily dead-set on getting access to a member pointer in this way, I would be fine just knowing the offset to the variable and having the Bar::value function perform trickery.
As a last resort, I suppose I could use a traits class since the would move the definition outside of the class, but I would prefer to be able to declare the typedef near the variable being used.
And, to answer the "why would you want to do that" question, this is all for an IoC container that closely represents the way MEF (C# works).
The specific compiler I am using is VC12, but it would be nice if VC11 supported it as well.
Thanks
EDIT:
Here are the error messages
1>------ Build started: Project: MemVarBug, Configuration: Debug Win32 ------
1> MemVarBug.cpp
1>memvarbug.cpp(20): error C2327: 'Foo::baz' : is not a type name, static, or enumerator
1>memvarbug.cpp(20): error C2065: 'baz' : undeclared identifier
1>memvarbug.cpp(20): error C2975: 'MV' : invalid template argument for 'Bar', expected compile-time constant expression
1> memvarbug.cpp(7) : see declaration of 'MV'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
you can do the following trick:
declare baz to be int[1];
array of size one, in this case calling baz will return the pointer and calling *baz will return the value.