|
You last visited: Today at 00:25
Advertisement
c++ array
Discussion on c++ array within the C/C++ forum part of the Coders Den category.
04/23/2015, 19:30
|
#1
|
elite*gold: 222
Join Date: Oct 2012
Posts: 2,361
Received Thanks: 3,388
|
c++ array
Code:
char titles[] = {
{"Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text"},
{"Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text"},
{"Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text", "Text"},
};
Code:
1>.\InstanceBaseEffect.cpp(683) : error C2440: 'initializing' : cannot convert from 'const char [15]' to 'char'
1> There is no context in which this conversion is possible
1>.\InstanceBaseEffect.cpp(683) : error C2078: too many initializers
1>.\InstanceBaseEffect.cpp(684) : error C2440: 'initializing' : cannot convert from 'const char [8]' to 'char'
1> There is no context in which this conversion is possible
1>.\InstanceBaseEffect.cpp(685) : error C2440: 'initializing' : cannot convert from 'const char [7]' to 'char'
1> There is no context in which this conversion is possible
1>.\InstanceBaseEffect.cpp(689) : error C2109: subscript requires array or pointer type
was mache ich falsch?
|
|
|
04/23/2015, 20:49
|
#2
|
elite*gold: 0
Join Date: Dec 2014
Posts: 442
Received Thanks: 211
|
Code:
#include <iostream>
#include <string>
#include <vector>
int main()
{
// Vector (bessere Lösung)
std::vector<std::string> MyTitles = { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" };
// Array
std::string MyArray[] = { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" };
std::cout << MyTitles.at(2) << " Oder " << MyTitles[2] << std::endl;
std::cout << MyArray[2] << std::endl;
return 0;
}
Ein char ist ein 1-Byte Character also z.B 'a', jedoch müsstest du hier ein char* (char pointer) machen oder viel besser mit einem vector und std::string
Siehe: error C2109: subscript requires array or pointer type
|
|
|
04/23/2015, 20:58
|
#3
|
elite*gold: 222
Join Date: Oct 2012
Posts: 2,361
Received Thanks: 3,388
|
Quote:
Originally Posted by _asm
Code:
#include <iostream>
#include <string>
#include <vector>
int main()
{
// Vector (bessere Lösung)
std::vector<std::string> MyTitles = { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" };
// Array
std::string MyArray[] = { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" };
std::cout << MyTitles.at(2) << " Oder " << MyTitles[2] << std::endl;
std::cout << MyArray[2] << std::endl;
return 0;
}
Ein char ist ein 1-Byte Character also z.B 'a', jedoch müsstest du hier ein char* (char pointer) machen oder viel besser mit einem vector und std::string
Siehe: error C2109: subscript requires array or pointer type
|
Code:
1>.\InstanceBaseEffect.cpp(683) : error C2552: 'titles' : non-aggregates cannot be initialized with initializer list
1> 'std::basic_string<_Elem,_Traits,_Ax>' : Types with a base are not aggregate
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>.\InstanceBaseEffect.cpp(684) : error C2552: 'titles' : non-aggregates cannot be initialized with initializer list
1> 'std::basic_string<_Elem,_Traits,_Ax>' : Types with a base are not aggregate
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>.\InstanceBaseEffect.cpp(685) : error C2552: 'titles' : non-aggregates cannot be initialized with initializer list
1> 'std::basic_string<_Elem,_Traits,_Ax>' : Types with a base are not aggregate
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
|
|
|
04/23/2015, 21:02
|
#4
|
elite*gold: 0
Join Date: Dec 2014
Posts: 442
Received Thanks: 211
|
Huh? Unter msvc compiled das ganze sauber, welchen Compiler nutzt du?
Hast du denn auch die <string> library inkludiert?
|
|
|
04/23/2015, 21:05
|
#5
|
elite*gold: 222
Join Date: Oct 2012
Posts: 2,361
Received Thanks: 3,388
|
Quote:
Originally Posted by _asm
Huh? Unter msvc compiled das ganze sauber, welchen Compiler nutzt du?
Hast du denn auch die <string> library inkludiert?
|
Ich nutze microsoft visual studio 2008 (für den metin2-sourcecode)
|
|
|
04/23/2015, 21:10
|
#6
|
elite*gold: 0
Join Date: Dec 2014
Posts: 442
Received Thanks: 211
|
Versuch mal das zu compilen:
Code:
#include <iostream>
#include <string>
#include <vector>
int main()
{
// Vector (bessere Lösung)
std::vector<std::string> MyTitles = { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" };
// Array
std::string MyArray[] = { "Text 0", "Text 1", "Text 2", "Text 3", "Text 4" };
std::cout << MyTitles.at(2) << " Oder " << MyTitles[2] << std::endl;
std::cout << MyArray[2] << std::endl;
return 0;
}
Und bist du dir auch wirklich sicher, dass du die String Lib included hast?
|
|
|
04/23/2015, 21:50
|
#7
|
elite*gold: 222
Join Date: Oct 2012
Posts: 2,361
Received Thanks: 3,388
|
Ja, hab sie included.
Na ja, habs so gemacht:
|
|
|
04/23/2015, 23:26
|
#8
|
elite*gold: 0
Join Date: Feb 2008
Posts: 2,754
Received Thanks: 1,748
|
Sind initializer lists nicht eigentlich erst C++11? Soweit ich mich erinnern kann, gabs das 2008 noch nicht.
|
|
|
04/24/2015, 01:03
|
#9
|
elite*gold: 0
Join Date: Jan 2009
Posts: 1,160
Received Thanks: 232
|
In deinem ersten Beispiel ist das Problem, dass du eine Variable vom Typ char[] definiert hast, du diese Variable aber mit char*[] füllen möchtest.
|
|
|
04/24/2015, 07:21
|
#10
|
elite*gold: 0
Join Date: Dec 2014
Posts: 442
Received Thanks: 211
|
Hab ich ihn auch gesagt, aber er hat es anscheind anders gemacht
|
|
|
04/24/2015, 11:02
|
#11
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
|
Quote:
du diese Variable aber mit char*[] füllen möchtest.
|
const char* oder const char[], um genau zu sein.
Das Problem hätte man auch einfach lösen können, indem man
Code:
char titles[] = /*...*/
zu
Code:
char titles[][] = /*...*/
//oder
const char titles[][] = /*...*/
geändert hätte.
std::string und std::vector wären natürlich schöner, aber das würde voraussetzen, dass man die Sprache kann bzw. sie lernen möchte. Hier geht es offensichtlich nur darum, einen kopierten Code zu kompilieren.
Und wenn man eine archaische IDE wie Visual Studio 2008 verwendet, funktionieren natürlich auch so tolle neue Sachen wie Initializer Lists nicht, weshalb deine Snippets natürlich nicht funktionieren können @asm.
|
|
|
|
Similar Threads
|
Array
09/20/2014 - Web Development - 8 Replies
ProtocolNode Object ( => message => Array ( => [email protected] => 1421333354-1233 => 0 => text => 1413364225 => Peter Pan ) => Array ( => ProtocolNode Object ( => body => Array ( ) => => Testlauf ) ) => )
Jemand eine Idee wie an die Array-Werte komme als Beispiel z.B an die Nachricht "Testlauf" oder den Namen "Peter Pan"?
|
Array Tabelle
09/15/2013 - AutoIt - 13 Replies
Hiho, ich brauche Bitte wieder mal Hilfe bei ner Array Tabelle, ich komm net so richtig weiter. Er gibt mir immer nur 3 Zeilen aus. :-(
Und zwar möchte ich den Body aus einen JS Script in eine Tabelle einlesen.
Ich habe es so gemacht:
Local $oTable = _IETableGetCollection($oIE, 0)
$array = _IETableWriteToArray($oTable, True)
_ArrayDisplay($array)
|
Array ausgeben
02/24/2012 - AutoIt - 3 Replies
Hey,
ich hab grad irwi ne denkblockade also:
Global $player = , , , , , , , ]
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
|
Array
06/18/2011 - AutoIt - 0 Replies
Array Fehler
Ich habe hier mal wieder einen Fehler den ich nicht beheben kann
#include "WebTcp.au3"
#include <Array.au3>
#Include <File.au3>
TCPStartup()
_AutoITObject_Startup()
|
All times are GMT +2. The time now is 00:25.
|
|