Inappropriate Expected Declaration Error

Expected declaration on switch declaration

You can't write switch rank { inside the struct directly , it should be inside a function init or any other custom one

public struct beltRank {
var rank = 0
var belt = ""
init(rank:Int) {
// write switch here
}
}

Xcode not recognizing declared variable?

You should access the property inside a function.

import UIKit

class FirstTableViewController:UITableViewController {

class Sport {

var name: String = "sport name"
var branches: NSArray = [" branches of this sport"]
}
var americanFootball = Sport()//This is a property of FirstTableViewController

override func viewDidLoad() {
americanFootball.name = "Another"
}

}

Compilation error using eclipse

In C++ size_t is declared in the <cstddef> header in the std namespace.

#include <cstddef>

int intFcn (const void *key, std::size_t table_size);

In C (and in C++ too), it's declared in <stddef.h>:

#include <stddef.h>

int intFcn (const void *key, size_t table_size);

If statement and && gives IDE error 'Syntax error on token =, = expected'

Testing equality

You need tips == true, or just if (guess<gval && tips) and if (tips) as you are testing a boolean.

= is an assignment operator, == is an equality operator. It is easy to mix the two up, but the difference is enormous...

Boolean assignment is also an (unexpected) equality test

You say "I tried using two if statements but when you say no, it still shows the tips.". The if statement expects an expression which must have type boolean. Any assignment expression (i.e. tips = true) evaluates as the new value assigned to the variable. From the JLS §15.26.1 Simple Assignment Operator:

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.

Therefore if (tips = true) is valid syntax, because tips = true is both an assignment and also a boolean expression which can therefore be used in an if.

In your case tips = true assigns true to tips (even if it started out false), then returns that new value of tips. The if sees the new value (true) and continues happily on. It is not therefore a test that tips was originally true. By example, this means that the following prints out the "Oops!" text:

boolean tips = false;
if (tips=true){
System.out.println("Oops! This tests as true, but that isn't what we wanted");
}

As you can see, conflating assignment and testing is generally a bad idea. It is usually a mistake if you use = in an if/do/while expression. Sometimes it can help with brevity but it is generally bad practice.

Why doesn't if (guess<gval && tips = true) work?

This is due to operator precedence. In the expression if (guess<gval && tips = true) the operator precedence is < then && and then =, so the compiler no longer evaluates the tips = true part as an assignment of true to tips. You could use brackets to contain tips = true, but as I said above you don't really want the assignment so you can ignore this detail .

Testing valid byte input

For the last part of your question - you say you get an invalid byte if someone inputs a letter. I'm guessing you get a java.util.InputMismatchException.

You can use Scanner.hasNextByte() to test if the input is a byte before consuming it. If that returns false you can consume and discard the input using nextLine(), print out an error message and loop again.

Depending on how you were writing your code you may also have seen a message from eclipse such as "The operator <= is undefined for the argument type(s) boolean, Boolean" or from javac such as "error: unexpected type ... required: variable, found: value". None of them tell you much other than your syntax is sufficiently messed up the compiler can't parse it.

error ; expected' when initialising multiple structs

Inside a strcut's defintion the elements' defintions are separated by ; not be ,. not the issue :}

  • Prior to C99 the initialiser list may not end by ,.confused by C99 change for enums. Ending it with a ; is always wrong. And there needs to be a = between the variable and its initialiser.

  • To initialise a struct's array member use curly braces ({}), not brackets ([]).

  • const uint_fast32_t pattern[]; is not a complete array definition.

    Use const uint_fast32_t pattern[MAX_SOMETHING]; instead.

error : storage class specified for parameter

Chances are you've forgotten a semicolon in a header file someplace. Make sure each line ends in ;

error: declaration of ‘operator*’ as non-function

I found that the most robust way to deal with friend operators in template classes was to put the full definition in the class itself:

//forward declaration
template<typename ElemType, size_t row, size_t col = row>
class matrix;
/*
template<typename ET, size_t R, size_t C>
matrix<ET, R, C> operator*(ET multier, matrix<ET, R, C> mattem);
*/
template<typename ElemType, size_t row, size_t col>
class matrix
...
//friend function prototype
friend matrix<ElemType, row, col> operator * (ElemType multier, matrix<ElemType, row, col> mattem){
return mattem * multier;
}
...
/*
//friend function definition
template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col> operator*(ElemType multier, matrix<ElemType, row, col> mattem)
{
return mattem*multier;
}
*/


Related Topics



Leave a reply



Submit