Что значит expected primary expression before token

Expected primary expression before ‘.’

I’m trying to write a simple class program. I am getting the error

Does this mean I have to use 2*square::length + 2*square::width ?

6 Answers 6

square is a type, not an object; instead of

(which is implicitly the same as:

which you may, or please may not, prefer for clarity).

2*square::length + 2*square::width would be valid syntax if length and width were

  • static members of square , or
  • members of some base class square , or
  • objects in some namespace square

Yes, the accurate form would be:

since square is a type, not an object.

In this context, it’s the same as:

However, since it’s the same object and the same type, you can just write:

You need to say square::perimeter() because you are defining a method of the square class itself. It may seem like you want to define it on a specific object, but you want it to be available to all instances of square, so you need to define it on a specific one.

The instance variables length and width on the other hand, pertain to a specific instance of a class and NOT the entire class itself (otherwise you would declare them as static ). This means that you can just refer to them directly instead of going through the class itself, and the compiler will know what variables you’re talking about. This is because width and length are defined in the same scope as the method, so you don’t need to give it special directions with :: to tell it where to find what its looking for. Hence:

to access instance variables, just name them:

what is the square in this function? square is a class. you use the . operator to acces members from objects. like sq.somefun();

so you perimeter function should be:

But isnt the length and width of square equal?

Not the answer you’re looking for? Browse other questions tagged c++ class or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.20.41044

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Что значит expected primary expression before token

lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, stepStones* [poisonNum / max] ) ;

is what is giving me the

error. Changing it to

lastNum = userTurn (poisonNum, max, lastNum, midlNum, enteredNum, stepStones* (poisonNum / max) ) ;

I guess this isn’t a variably sized array in that you can’t modify its size once its been declared, but I’ve always wondered why people tell me not to do this. I use this all the time.

My second question is WTF. How in the heck did you manage to declare functions inside main()?

why will the following compile?

It won’t in standards compliant compilers.

Some compilers allow it as an extension, but it’s not legal C++. If you do it, you run the risk of your code not compiling for other people.

Источник

ошибка: ожидаемое первичное выражение до токена ‘=’ и многие другие

Либо я слепой, либо ошибки нет. Я думаю, что это был бы, вероятно, первый выбор. Пожалуйста, помогите мне найти иголку в стоге сена. Это часть моего списка ошибок:

и это часть моего кода:

У меня есть много подобных ошибок во всем коде, поэтому я думаю, что есть что-то вроде пропуска столбца или чего-то другого. Вы видите это? Я не.

Решение

Макрос не является переменной. Это не имеет значения. Это функция предварительной обработки.

Причина, по которой у вас везде появляются синтаксические ошибки, заключается в том, что ERR_OK и тому подобное были заменены = 0; скорее, чем 0 например.

Другие решения

Другие ответы верны, что проблема заключается в символических константах

Тем не менее, в C ++ есть лучший способ исправить это:

Или C и C ++ оба позволяют

Ваш #define s неверны, они должны выглядеть следующим образом, например:

То есть они не должны иметь = и не заканчивается точкой с запятой. #define это директива препроцессора. Они не подчиняются тем же правилам синтаксиса, что и остальная часть C ++. В частности, директива препроцессора завершается новой строкой, а не точкой с запятой.

Это на самом деле определяет ERR_PARAMS быть = 1; , Если потом заменить ERR_PARAMS с = 1; в своем коде вы увидите, почему всплывают некоторые ошибки. Например, рассмотрим эту строку:

Если вы замените ERR_PARAMS здесь вы получите:

Источник

Читайте также:  Режущая система роторная что это значит
Оцените статью