c++ array

04/23/2015 19:30 Mr. 'Avenue™#1
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 _asm#2
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 Mr. 'Avenue™#3
Quote:
Originally Posted by _asm View Post
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 _asm#4
Huh? Unter msvc compiled das ganze sauber, welchen Compiler nutzt du?
Hast du denn auch die <string> library inkludiert?
04/23/2015 21:05 Mr. 'Avenue™#5
Quote:
Originally Posted by _asm View Post
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 _asm#6
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 Mr. 'Avenue™#7
Ja, hab sie included.

Na ja, habs so gemacht: [Only registered and activated users can see links. Click Here To Register...]
04/23/2015 23:26 Computerfreek#8
Sind initializer lists nicht eigentlich erst C++11? Soweit ich mich erinnern kann, gabs das 2008 noch nicht.
04/24/2015 01:03 Delinquenz#9
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 _asm#10
Hab ich ihn auch gesagt, aber er hat es anscheind anders gemacht
04/24/2015 11:02 MrSm!th#11
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.