Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: c++ code: Fout in de declaratie van een array met items  (gelezen 1076 keer)

Offline bart85

  • Lid
c++ code: Fout in de declaratie van een array met items
« Gepost op: 2015/01/27, 17:16:08 »
Op onderstaande manier wou ik een array declareren:

#define players_size 100

struct player
{
std::string name;
}

//array aanmaken van het type player
player players [players_size];
« Laatst bewerkt op: 2015/01/27, 17:20:01 door bartveurink »
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Offline bart85

  • Lid
Re: c++ code: Fout in de declaratie van een array met items
« Reactie #1 Gepost op: 2015/01/27, 17:45:23 »
De volgende error krijg ik steeds:

competitie.cpp:14:8: error: expected initializer before ‘players’
 player players [players_size];
« Laatst bewerkt op: 2015/01/27, 17:57:53 door bartveurink »
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Offline bart85

  • Lid
Re: c++ code: Fout in de declaratie van een array met items
« Reactie #2 Gepost op: 2015/01/28, 15:07:08 »
Ik snap er niks van. Krijg meerdere foutmeldingen.
Hier onder de volledige code:
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

#define players_size 100

struct player
{
string name;
}

player* playerlist = new player[players_size];


int n = 0;

void load_players ()
{
//inlezen van player_list
ofstream myplayers;
ofstream.open ("players",ios::in);
if (myplayers.is_open())
{
string line;
while (n < players_size && getline(myplayers, line))
{
playerlist[n].name = line;
n++;
}
}
}

int main()
{
load_players ();
int m = n * (n - 1);

}
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Re: c++ code: Fout in de declaratie van een array met items
« Reactie #3 Gepost op: 2015/01/28, 21:12:37 »
"meerdere foutmeldingen" is wel erg vaag. Misschien is het handig om die toch wel te vermelden. En welke regel gebruik je om te compileren?

Heb eens Google geraadpleegd over je eerste foutmelding. Volgens mij ontbreekt er gewoon een puntkomma na de struct-declaratie (struct player).
« Laatst bewerkt op: 2015/01/28, 21:32:09 door erik1984 »

Offline bart85

  • Lid
Re: c++ code: Fout in de declaratie van een array met items
« Reactie #4 Gepost op: 2015/01/29, 09:38:08 »
$ g++ -o comp competitie.cpp
competitie.cpp:11:1: error: expected unqualified-id before ‘{’ token
 {
 ^
competitie.cpp:14:40: error: elements of array ‘player playerlist [100]’ have incomplete type
 player playerlist[players_size] = {NULL};
                                        ^
competitie.cpp:14:40: error: storage size of ‘playerlist’ isn’t known
competitie.cpp: In function ‘void load_players()’:
competitie.cpp:24:10: error: expected unqualified-id before ‘.’ token
  ofstream.open ("players",ios::in);
          ^
competitie.cpp:28:53: error: no matching function for call to ‘getline(std::ofstream&, std::string&)’
   while (n < players_size && getline(myplayers, line))
                                                     ^
competitie.cpp:28:53: note: candidates are:
In file included from /usr/include/c++/4.8/cstdio:42:0,
                 from /usr/include/c++/4.8/fstream:41,
                 from competitie.cpp:3:
/usr/include/stdio.h:678:20: note: __ssize_t getline(char**, size_t*, FILE*)
 extern _IO_ssize_t getline (char **__restrict __lineptr,
                    ^
/usr/include/stdio.h:678:20: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from competitie.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:2793:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
     getline(basic_istream<_CharT, _Traits>& __is,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2793:5: note:   template argument deduction/substitution failed:
competitie.cpp:28:53: note:   ‘std::ofstream {aka std::basic_ofstream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
   while (n < players_size && getline(myplayers, line))
                                                     ^
In file included from /usr/include/c++/4.8/string:53:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from competitie.cpp:1:
/usr/include/c++/4.8/bits/basic_string.tcc:1068:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
     getline(basic_istream<_CharT, _Traits>& __in,
     ^
/usr/include/c++/4.8/bits/basic_string.tcc:1068:5: note:   template argument deduction/substitution failed:
competitie.cpp:28:53: note:   ‘std::ofstream {aka std::basic_ofstream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
   while (n < players_size && getline(myplayers, line))
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Re: c++ code: Fout in de declaratie van een array met items
« Reactie #5 Gepost op: 2015/01/29, 12:37:01 »
Dit is wat Clang zegt:

<stdin>:13:2: error: expected ';' after struct
}
 ^
<stdin>:15:9: warning: no previous extern declaration for non-static variable
      'playerlist' [-Wmissing-variable-declarations]
player* playerlist = new player[players_size];
        ^
<stdin>:15:9: warning: declaration requires a global constructor
      [-Wglobal-constructors]
player* playerlist = new player[players_size];
        ^            ~~~~~~~~~~~~~~~~~~~~~~~~
<stdin>:18:5: warning: no previous extern declaration for non-static variable
      'n' [-Wmissing-variable-declarations]
int n = 0;
    ^
<stdin>:20:6: warning: no previous prototype for function 'load_players'
      [-Wmissing-prototypes]
void load_players ()
     ^
<stdin>:24:10: error: cannot use dot operator on a type
        ofstream.open ("players",ios::in);
                ^
<stdin>:28:30: error: no matching function for call to 'getline'
                while (n < players_size && getline(myplayers, line))
                                           ^~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/istream:1633:1: note:
      candidate template ignored: could not match 'basic_istream' against
      'basic_ofstream'
getline(basic_istream<_CharT, _Traits>& __is,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/istream:1584:1: note:
      candidate function template not viable: requires 3 arguments, but 2 were
      provided
getline(basic_istream<_CharT, _Traits>& __is,
^
/usr/include/stdio.h:442:9: note: candidate function not viable: requires 3
      arguments, but 2 were provided
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restri...
        ^
<stdin>:39:6: warning: unused variable 'm' [-Wunused-variable]
        int m = n * (n - 1);
            ^
5 warnings and 3 errors generated.
I use a Unix-based system, that means I'll get laid as often as I have to reboot.
LibSylph
SeySayux.net

Re: c++ code: Fout in de declaratie van een array met items
« Reactie #6 Gepost op: 2015/01/29, 20:33:35 »
error: expected ';' after structDat bedoelde ik dus, heb je dat inmiddels aangepast? Maar zo te zien niet het enige probleem.

Offline bart85

  • Lid
Re: c++ code: Fout in de declaratie van een array met items
« Reactie #7 Gepost op: 2015/02/12, 14:18:30 »
Hier is de code zoals hij moet zijn.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int n = 25;

class MyPlayer
{
public: string name;
};

int main()
{
MyPlayer players[n];
  string line;
  ifstream myfile ("players");

int i = 0;
  if (myfile.is_open())
  {
    while ( getline (myfile,line) && i < n)
    {
    players[0].name = line;
i += 1;
    }
    myfile.close();
  }
}
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.