error C2440: '=' : cannot convert from 'std::string []' to 'std::string []' - c++

now what is wrong with this code!
Header:
#pragma once
#include <string>
using namespace std;
class Menu
{
public:
Menu(string []);
~Menu(void);
};
Implementation:
#include "Menu.h"
string _choices[];
Menu::Menu(string items[])
{
_choices = items;
}
Menu::~Menu(void)
{
}
compiler is complaining:
error C2440: '=' : cannot convert from 'std::string []' to 'std::string []'
There are no conversions to array types, although there are conversions to references or pointers to arrays
there is no conversion! so what is it on about?
please help, just need to pass a bloody array of strings and set it to Menu class _choices[] attribute.
thanks

Array's cannot be assigned, and your arrays have no sizes anyway. You probably just want a std::vector: std::vector<std::string>. This is a dynamic array of strings, and can be assigned just fine.
// Menu.h
#include <string>
#include <vector>
// **Never** use `using namespace` in a header,
// and rarely in a source file.
class Menu
{
public:
Menu(const std::vector<std::string>& items); // pass by const-reference
// do not define and implement an empty
// destructor, let the compiler do it
};
// Menu.cpp
#include "Menu.h"
// what's with the global? should this be a member?
std::vector<std::string> _choices;
Menu::Menu(const std::vector<std::string>& items)
{
_choices = items; // copies each element
}

You can't define the array as string _choices[], that defines an array with unknown size, which is illegal.
If you change it to string * _choices it will work just fine (though be aware it will only copy a pointer to the array, and not clone it all).
Also, don't you want _choices to be a field of the class, instead of a global?

Related

vector definition in struct (c++)

A very basic question, but i haven't found the answer yet.
I wrote an Application which is using an Array with a constant predefined Value which defines the size of this array. So but now i want to change this so that the "list" can theoretically be endless (i know that it would be practically impossible). For that I want to use a vector. But when I type the following it gives me an Error:
edit(2): Wrote accidentially const and a wrong parameter for push_back funktion, heres the final version, which gives the error.
#include "stdafx.h"
#include "string"
#include "vector"
using namespace std;
struct Board {
vector <string> myVector;
myVector.push_back("foo");
};
Error Message:
<error-type> Board::myVector
This declaration has no storage class or type specifier.
My thought was that vectors dont work in structs. I heard that structs are a plain C thing and vectors are more a C++ thing, maybe thats why it is so? But actually I have no clue, thats why I am asking here :)
edit (1):
I just gave you the visual studio error, maybe I should give you the compiler errors..:
error C3927: '->': trailing return type is not allowed after a non-function declarator
error C3484: syntax error: expected '->' before the return type
error C3613: missing return type after '->' ('int' assumed)
error C3646: 'push_back': unknown override specifier
error C2059: syntax error: '('
error C2238: unexpected token(s) preceding ';'
The problem are as followings:
(corrected in OP question) myVector is defined const
myVector.push_back(1); is not in any function body.
(corrected in OP question) Value passed to myVector.push_back(1); is int but vector is of type string
Change it as following. See example program working here:
#include "string"
#include "vector"
#include "iostream"
using namespace std;
struct Board {
vector<string> myVector;
void push_back(string val)
{
myVector.push_back(val);
}
void print()
{
for (auto it = myVector.begin(); it != myVector.end(); ++it)
cout << " | " << *it;
}
};
int main()
{
Board b;
b.push_back("Value 1");
b.push_back("Value 2");
b.print();
return 0;
}
UPDATE:
(can you actually use push_back for a vector in a struct without creating an extra function?)
No. structure can have only data members and member functions. but you can use initializer-list to initialize vector as following:
vector<string> myVector{"IVal 1", "IVal 1"};
If you wants to put the initlize value always at the end then use vector.insert() instead of vector.push_back().

Confusion about constructors - expected a ';'

Instead of putting my class in the same file as my main function, I'm trying to use a #include. Though, when I do this, I get an error for my constructor. This is my input.cpp file:
#ifndef input
#define input
using namespace std;
#include <string>
#include <iostream>
class input
{
public:
input(int sent)
{
s = sent;
}
void read();
void store(string s);
private:
int s;
};
#endif
This is my main function:
#include <iostream>
#include <string>
using namespace std;
#include "input.cpp"
int main()
{
cout<<"Hello, please enter your input"<<endl;
string sent;
getline(cin, sent);
cout<<sent;
input1 *newinput = new input1("hello");
system("pause");
return 0;
}
The error I'm getting is
"intelliSense expected a ';'"
in the body of my constructor. Though, when I copy / paste the class directly into my main.cpp file, the error goes away. Any idea on what is causing this?
Do no use using namespace in headers
You have input as macro constant and name of class is the same. I afraid it's the root your problem.
Prefer to use constructor initialization lists input(int sent) : s(sent) {}
UPDT
you may need constructor able to accept string as parameter input(const std::string& str1) : str(str1) {} where str is class member to handle string data.
You defined the constructor as having one parameter of type int
input(int sent)
{
s = sent;
}
but try to call it passing as an argument a string literal
input *newinput = new input("hello");
The string literal that has type const char[6] can not be implicitly converted to type int and the class has no other constructor that accepts character arrays as arguments.
EDIT: You changed you original post several times so it is not clear now whether using name input1 in stateent
input1 *newinput = new input1("hello");
is a typo or it is some other type.
Also you have a macro definition with the same name as the class name
#ifndef input
#define input
Change either the macro name or the class name.

Stl container of std::basic_string type

#ifndef UNICODE
#define UNICODE
#endif
#include <iostream>
#include <queue>
#include <stdio.h>
#include <Windows.h>
#include <string>
using namespace std;
int __cdecl main()
{
std::queue<std::basic_string<TCHAR>> results;
results.push(TEXT("Hello world! ♥☻☺"));
wcout<<results.front();
delete [] results.front();
system("pause");
return 0;
}
Error 1 error C2440: 'delete' : cannot convert from
'std::basic_string<_Elem,_Traits,_Ax>' to 'void
*' C:\Users\Tomek\Documents\Visual Studio 2010\Solutions\clean_rough_draft\clean_rough_draft\main.cpp 20 1 clean_rough_draft
Why such error is being thrown and how to fix it?
Your first problem was you forgot to include <string>.
Your current problem is your delete makes no sense. Your string isn't dynamically allocated, and front() returns a reference to it anyway. So, you're trying to call array delete on something that isn't an array (a string is an object that encapsulates an array) and that wasn't dynamically allocated in the first place (and on a reference instead of a pointer).

sending a list of string from C# to C++ causes errors

I want to send a C# a list of string into C++ code using C++/CLI:
in C++, I put this in a constructor:
#include <string>
public:
MyAlgorithm(array<std::string>^ listAlgorithms);
But I got this compilation error:
error C2691: 'std::string' : a managed array cannot have this element
type
And in the implementation I have:
MyAlgorithm(array<std::string>^ listAlgorithms)
{
pin_ptr<std::string> algorithms = &listAlgorithms[0];
std::string* unmanagedAlgorithms = algorithms;
}
And I got this error:
error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'cli::pin_ptr<Type>'
How should I correct them?
Thanks in advance.
#include <string>
#include <msclr/marshal_cppstd.h>
MyAlgorithm(array<String^>^ listAlgorithms)
{
std::vector<std::string> unmanagedAlgorithms(listAlgorithms->Length);
for (int i = 0; i < listAlgorithms->Length; ++i)
{
auto s = listAlgorithms[i];
unmanagedAlgorithms[i] = msclr::interop::marshal_as<std::string>(s);
}
}
or
std::vector<std::string> unmanagedAlgorithms;
for each (auto algorithm in listAlgorithms)
{
unmanagedAlgorithms.push_back(msclr::interop::marshal_as<std::string>(algorithm));
}
or first string only
String^ managedAlgorithm = listAlgorithms[0];
std::string unmanagedAlgorithm = msclr::interop::marshal_as<std::string>(managedAlgorithm);
Y'all can't use classes when defining a managed array. If you're looking to use the std::string class, you're probably best going with something like std::vector.
PS: How come you don't do?
using namespace std;

Explain the error: ISO C++ forbids declaration of `Personlist' with no type

I have a class which is going to handle an array of objects of another class I've created earlier (which works fine). The problem appears when I try to create an object of my List-class.
This is the header of the list-class:
#ifndef personlistH
#define personlistH
#include "Person.h"
#include <iomanip>
#include <iostream>
#define SIZE 10
namespace std {
class PersonList {
private:
Person persons[SIZE];
int arrnum;
string filename;
public:
Personlist();
};
}
#endif
This is the main function:
#include <iostream>
#include "PersonList.h"
using namespace std;
int main() {
PersonList personlist;
return 0;
}
The error my compiler is giving me is the following:
error: "27 \PersonList.h ISO C++ forbids declaration of `Personlist'
with no type"
I've searched for answers but as I'm quite new to C++ it's been a bit confusing and I haven't found any fitting yet. It would be great if you could explain this error for me.
You have the wrong capitalisation on your constructor declaration. You have Personlist(); but need PersonList();. Because what you have isn't equal to the class name it is considered a function rather than a constructor, and a function needs a return type.
Do not add your own types to the standard namespace(std), instead create your own namespace and define your class inside it.
//PersonList.h
namespace PersonNamespace
{
class PersonList
{
//members here
};
}
//Main.cpp
using namespace PersonNamespace;
The actual error is that you made a typo in Personlist instead of PersonList
The error is because you got the capitalisation wrong when you declared the constructor; it should be PersonList() not Personlist().
Also, you should never declare your own classes in the std namespace; that's reserved for the standard library. You shoud make up your own namespace name, and put your things in that.