Expression must have constant value.

0

Witam,

Od niedawna próbuje uporać się z jednym błędem w moim kodzie.
Funkcja:

  bool readPacket(const FileStreamPtr& fin){
    try{
        if (!fin){
          stdext::throw_exception("Error: FileStreamPtr not found");
          return false;
        }

        uint8_t packetCode = fin->getU8();
        if (packetCode != 255){
          stdext::throw_exception(stdext::format("Error: FileStreamPtr packetCode = %d", packetCode));
          return false;
        }

        id = fin->getU32();
        time = fin->getU32();
        int16_t msgSize = fin->getU16();
        uint8 data[msgSize];
        fin->read(&data[0], msgSize);
        msg = OutputMessagePtr(new OutputMessage);
        msg->addBuffer(msgSize, &data[0]);


    }catch(stdext::exception& e) {
      g_logger.error(stdext::format("Error in readFrame: %s", e.what()));
      return false;
    }

    return true;
  }

  OutputMessagePtr getMsg() { return msg; }
  uint32_t getTime() { return time; }
  uint32_t getId() { return id; }

  OutputMessagePtr msg;
  uint32_t time = 0;
  uint32_t id = 0;
};

Bład w linijce:

uint8 data[msgSize];
Expression must have a constant value.

title

5

Wielkość tablicy musi być stałą kompilacji. Jak ma być dynamiczna to użyj std::vector.

0

Dodałem cos takiego do filestream.h

std::vector<int16_t> msgSize;

Nadal to samo.

1

Użyj std::vector zamiast tablicy...

0

Możesz pokazać jak ma to wyglądać?

1

Tablica ma nazwę data, a nie msgSize. Wywaliłeś nie to co trzeba.

0

Coś takiego?

int16_t msgSize = fin->getU16();
std::vector<int> msgSize;
fin->read(0, msgSize);
msg = OutputMessagePtr(new OutputMessage);
msg->addBuffer(msgSize, 0);
1

Chodziło o to, byś tablicę o nazwie data zamienił na wektor o nazwie data, a nie msgSize.

1
        int16_t msgSize = fin->getU16();
        std::vector<uint8> data(msgSize);
0

Mam nadzieje ze dobrze zrozumialem:

int16_t msgSize = fin->getU16();
std::vector<uint8_t> data;
fin->read(&data[0], msgSize);
msg = OutputMessagePtr(new OutputMessage);
msg->addBuffer(msgSize, &data[0]);

Jak coś się nie zgadza to daj znać. W visualu ten kody wygląda na poprawny.

1

Nie jest poprawny, bo data ma wielkość 0. Użyj mojego kodu.

0

Dzieki, coś kompilacja nie idzie nie moge ogarnąc dlaczego.

		uint16_t msgSize = fin->getU16();
		std::vector<uint8> data(msgSize);
        fin->read(&data[0], msgSize);
        msg = OutputMessagePtr(new OutputMessage);
        msg->addBuffer(msgSize, &data[0]);

void addBuffer(uint16 length, uint8* bytes);
std::string getBuffer() { return std::string((char*)m_buffer + m_headerPos, m_messageSize); }
error LNK2001: unresolved external symbol "public: void __thiscall OutputMessage::addBuffer(unsigned short,unsigned char *)" (?addBuffer@OutputMessage@@QAEXGPAE@Z)
0

void addBuffer(uint16 length, uint8* bytes); to funkcja wolna
void __thiscall OutputMessage::addBuffer(unsigned short,unsigned char *) to funkcja klasy (niestatyczna). Jak chcesz zdefiniować funkcję klasy, to zdefiniuj funkcję klasy, a nie wolną.

1 użytkowników online, w tym zalogowanych: 0, gości: 1