关于“指针在任何情况下都可进行>, <, >=, <=, = =运算”的判断

本文探讨了关于指针是否可以在任何情况下进行比较运算的问题。通过分析两种观点并给出实验代码,证实了不同类型指针直接比较会导致编译错误,需要进行类型转换。

在《求职之道》上看到了“指针在任何情况下都可进行>, <, >=, <=, = =运算”的判断题,

脑中无解,便去百度搜了下,可是发现大多数人都说这个命题是正确的,当然也有不同意见的。


意见一:
指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址,可看成整型变量。 
如果仅仅是指针比较,其实就是整型变量比较,当然任何情况下都可以进行比较运算

意见二:
1.指针是地址就和一般数据一样,但地址不可以进行比较运算吧。所以应该是指针变量。
2.是在任何的情况下也应该不对,如果是类型不匹配呢。

然后自己写了如下代码:

int *a = (int*)0x00007788;
char *b = (char*)0x00007789;
if(a == b) cout<<"pa == pb"<<endl;
if(a > b) cout<<"pa > pb"<<endl;
if(a < b) cout<<"pa < pb"<<endl;

编译结果:

a.cpp:15:8: 错误: 在不同的指针类型‘int*’和‘char*’之间的比较需要一个类型转换 [-fpermissive]
a.cpp:16:7: 错误: 在不同的指针类型‘int*’和‘char*’之间的比较需要一个类型转换 [-fpermissive]
a.cpp:17:7: 错误: 在不同的指针类型‘int*’和‘char*’之间的比较需要一个类型转换 [-fpermissive]


所以我个人觉得就这么简单的一个程序就足以说明了上面的命题是错误的。





报错:'!=', '%', '&&', '(', '*', '+', '+=', '-', '.', '/', <, <=, '==', '>', '>=', '?', '?.', '?:', and, div, eq, ge, gt, le, lt, mod, ne, or, '||' or '}' expected, got '.' <default conditional expression>, <expression>, <ognl ternary expression> or '{' expected, got 'currentPage-1' <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:sec="http://www.w3.org/1999/xhtml" layout:decorate="~{layout}"> <head> <title>图书列表</title> </head> <body> <div layout:fragment="content"> <div class="d-flex justify-content-between align-items-center mb-4"> <h2><i class="fas fa-list"></i> 图书列表</h2> <a th:href="@{/books/upload}" class="btn btn-primary" sec:authorize="isAuthenticated()"> <i class="fas fa-upload"></i> 上传图书 </a> </div> <!-- 搜索和筛选 --> <div class="card mb-4"> <div class="card-body"> <form th:action="@{/books}" method="get" class="row g-3"> <div class="col-md-6"> <input type="text" name="search" class="form-control" th:value="${param.search}" placeholder="搜索图书标题、作者或描述..."> </div> <div class="col-md-3"> <select name="format" class="form-select"> <option value="">所有格式</option> <option th:each="format : ${T(com.booksystem.entity.BookFormat).values()}" th:value="${format}" th:text="${format}" th:selected="${param.format == format}"> </option> </select> </div> <div class="col-md-3"> <button type="submit" class="btn btn-outline-primary w-100"> <i class="fas fa-search"></i> 搜索 </button> </div> </form> </div> </div> <!-- 图书网格 --> <div th:if="${books != null and !books.empty}"> <div class="row"> <div th:each="book : ${books}" class="col-xl-3 col-lg-4 col-md-6 mb-4"> <div class="card book-card h-100"> <div class="book-cover-container position-relative"> <div class="book-cover bg-light" th:style="'background-image: url(' + ${book.coverImagePath} + ');'"> <th:block th:if="${book.coverImagePath == null}"> <i class="fas fa-book fa-2x text-muted"></i> </th:block> </div> <span class="position-absolute top-0 end-0 m-2 badge bg-primary" th:text="${book.format}">格式</span> </div> <div class="card-body d-flex flex-column"> <h6 class="card-title" th:text="${book.title}">书名</h6> <p class="card-text text-muted small mb-2" th:text="${book.author}">作者</p> <p class="card-text small text-muted flex-grow-1" th:text="${#strings.abbreviate(book.description, 100)}">描述</p> <div class="mt-auto"> <div class="d-flex justify-content-between align-items-center"> <small class="text-muted" th:text="${#temporals.format(book.uploadDate, 'yyyy-MM-dd')}"> 上传日期 </small> <div> <a th:href="@{/books/preview/{id}(id=${book.id})}" class="btn btn-sm btn-outline-primary" title="预览"> <i class="fas fa-eye"></i> </a> <a th:href="@{/books/download/{id}(id=${book.id})}" class="btn btn-sm btn-outline-success" title="下载"> <i class="fas fa-download"></i> </a> </div> </div> </div> </div> </div> </div> </div> <!-- 分页 --> <nav th:if="${totalPages > 1}" class="mt-4"> <ul class="pagination justify-content-center"> <li class="page-item" th:classappend="${currentPage == 0} ? 'disabled'"> <a class="page-link" th:href="@{/books(page=0, search=${param.search}, format=${param.format})}"> 首页 </a> </li> <li class="page-item" th:classappend="${currentPage == 0} ? 'disabled'"> <a class="page-link" th:href="@{/books(page=${currentPage-1}, search=${param.search}, format=${param.format})}"> 上一页 </a> </li> <li th:each="page : ${#numbers.sequence(0, totalPages-1)}" class="page-item" th:classappend="${page == currentPage} ? 'active'"> <a class="page-link" th:href="@{/books(page=${page}, search=${param.search}, format=${param.format})}" th:text="${page+1}">1</a> </li> <li class="page-item" th:classappend="${currentPage == totalPages-1} ? 'disabled'"> <a class="page-link" th:href="@{/books(page=${currentPage+1}, search=${param.search}, format=${param.format})}"> 下一页 </a> </li> <li class="page-item" th:classappend="${currentPage == totalPages-1} ? 'disabled'"> <a class="page-link" th:href="@{/books(page=${totalPages-1}, search=${param.search}, format=${param.format})}"> 末页 </a> </li> </ul> </nav> </div> <!-- 空状态 --> <div th:if="${books == null or books.empty}" class="text-center py-5"> <i class="fas fa-book fa-4x text-muted mb-3"></i> <h4 class="text-muted" th:text="${param.search != null} ? '没有找到相关图书' : '暂无图书'"></h4> <p class="text-muted" th:text="${param.search != null} ? '尝试调整搜索条件' : '成为第一个分享图书的人吧!'"></p> <a th:href="@{/books/upload}" class="btn btn-primary" sec:authorize="isAuthenticated()"> <i class="fas fa-upload"></i> 上传图书 </a> <a th:href="@{/books}" class="btn btn-outline-secondary" th:if="${param.search != null}"> <i class="fas fa-times"></i> 清除搜索 </a> </div> </div> <div layout:fragment="scripts"> <script> document.addEventListener('DOMContentLoaded', function() { // 图书列表特定的JS代码 }); </script> </div> </body> </html>
最新发布
12-01
<think> 我们正在处理Thymeleaf模板引擎中的两个语法错误。错误信息表明解析器在期望某些运算符或表达式时遇到了不符合预期的字符。具体来说: 1. 第一个错误:在期望运算符(如 '!=', '%' 等)或 '}' 的地方,却遇到了 '.' 字符。 2. 第二个错误:在期望条件表达式、表达式、OGNL三元表达式或 '{' 的地方,遇到了字符串 'currentPage-1'。 根据错误信息,问题很可能出现在Thymeleaf表达式的编写上。Thymeleaf使用的是Spring EL(SpEL)或OGNL表达式,因此需要遵循特定的语法规则。 常见原因及解决方法: 1. **错误使用点号(.)**: 在Thymeleaf中,点号通常用于访问对象的属性。但是,在表达式的某些位置,点号的使用可能受到限制,或者因为上下文不允许而报错。 例如,在条件表达式中,如果直接使用点号而没有正确的上下文,可能会引发错误。 错误示例:`${user.name}` 在Thymeleaf中应该使用 `th:text="${user.name}"`,但有时在复杂的表达式中,如条件判断,点号的使用需要特别注意。 另外,注意在数字字面量中,点号用于表示小数,但整数部分不能直接以点号开头(如`.5`是不允许的,应该写成`0.5`)。 2. **表达式不完整或格式错误**: 第二个错误提到期望条件表达式等,但得到了`currentPage-1`。这通常是因为在应该使用表达式的地方直接写了一个字符串(或变量名),而没有使用正确的表达式语法。 例如,在Thymeleaf中,如果我们想使用一个表达式作为属性值,我们需要用`${...}`、`*{...}`、`#{...}`或`@{...}`包裹起来。如果直接写`currentPage-1`,解析器会认为这是一个字符串,而不是表达式,因此会报错。 正确的做法:在需要表达式的地方,将变量和运算符放在表达式内。例如: 错误写法:`th:if="currentPage-1 > 0"` 正确写法:`th:if="${currentPage - 1 > 0}"` 或者更清晰的写法是使用括号:`th:if="${(currentPage - 1) > 0}"` 另外,注意在Thymeleaf中,变量名不能包含特殊字符(如减号`-`),除非是作为运算符。所以`currentPage-1`会被解析为“变量currentPage减去1”,但是如果在表达式中直接写`currentPage-1`而没有用表达式符号包裹,解析器就会困惑。 3. **三元运算符的使用**: 三元运算符(`condition ? trueValue : falseValue`)在Thymeleaf中可以使用,但是必须确保条件、真值和假值都是有效的表达式部分。 4. **使用括号明确优先级**: 当表达式较为复杂时,使用括号可以避免运算符优先级问题,同时也能让解析器更清晰地理解表达式结构。 5. **检查引号的使用**: 确保字符串值用引号括起来,而变量名不要用引号。 具体到你的错误信息: - 第一个错误:遇到了点号。请检查在表达式中是否有点号使用不当的地方,比如在数字字面量开头(如`.5`,应改为`0.5`),或者在表达式中连续使用点号(如`user..name`)。 - 第二个错误:在期望表达式的地方遇到了`currentPage-1`。这很可能是因为在一个应该写表达式的位置,你直接写了`currentPage-1`而没有用表达式符号包裹,或者在表达式中错误地使用了减号(可能是想表示变量名,但减号在表达式中是运算符)。 解决方案步骤: 1. **检查表达式是否被正确包裹**: 确保所有动态表达式都使用`${...}`(或`*{...}`等)包裹。例如: ```html <!-- 错误 --> <a th:href="@{/page/currentPage-1}">Previous</a> <!-- 正确:在表达式中进行计算 --> <a th:href="@{/page/${currentPage - 1}}">Previous</a> ``` 2. **避免在变量名中使用连字符**: 不要使用像`currentPage-1`这样的变量名,因为减号是运算符。应该使用驼峰命名法或下划线,例如`currentPageMinusOne`(但这通常不是好的做法,而是应该直接计算)。 3. **在表达式中进行数学运算**: 如果你想要在URL中使用当前页减1,那么应该在表达式内部进行运算: ```html <a th:href="@{'/page/' + ${currentPage - 1}}">Previous</a> ``` 或者使用预处理: ```html <a th:href="@{/page/{page}(page=${currentPage - 1})}">Previous</a> ``` 4. **检查条件表达式**: 对于条件表达式,确保条件部分是一个完整的布尔表达式,并且使用括号来明确运算顺序。 例如,避免:`th:if="currentPage-1 > 0"`(虽然这个在表达式中是允许的,但要注意`currentPage-1`作为表达式的一部分,整个表达式要在`${}`内) 正确写法:`th:if="${currentPage > 1}"`(因为上一页存在意味着当前页至少为2,那么上一页就是1) 5. **点号问题的解决**: 如果错误是因为点号引起的,检查是否在表达式中使用了点号作为对象的属性访问,但该对象可能为null。使用安全导航运算符(`?.`)可以避免空指针,但注意安全导航运算符的使用位置。 例如:`${user?.name}` 如果user为null,则整个表达式返回null,而不会报错。 6. **检查模板中的括号和花括号是否匹配**。 7. **如果使用内联表达式**,注意使用`[[...]]`或`[(...)]`,并且内部同样遵循表达式规则。 根据你提供的错误信息,我推测你的模板中可能有类似这样的代码: 错误示例1(点号问题): ```html <span th:text="${user.name}"></span> <!-- 如果这里正确,可能不是这个简单情况 --> <!-- 可能出现在复杂表达式中,如 --> <span th:text="${user.name.toUpperCase()}"></span> <!-- 如果name为null,这里会报错,但错误信息不同 --> <!-- 或者 --> <span th:text="${.5}"></span> <!-- 错误,应该写0.5 --> ``` 错误示例2(currentPage-1问题): ```html <!-- 错误 --> <a th:href="@{/page/currentPage-1}">Previous</a> <!-- 正确写法1:使用路径变量 --> <a th:href="@{/page/{page}(page=${currentPage - 1})}">Previous</a> <!-- 正确写法2:拼接字符串 --> <a th:href="@{'/page/' + (currentPage - 1)}">Previous</a> ``` 请检查你的模板,特别是包含`currentPage-1`和点号的地方,按照上述方法进行修改。 如果问题仍然存在,请提供相关的模板代码片段,以便更准确地定位问题。 </think> 针对Thymeleaf报错 `!=, '%', '&&', ... expected, got '.'` 和 `<expression> expected, got 'currentPage-1'` 的问题,以下是根本原因和解决方案: ### 错误原因分析 1. **点号运算符错误(got '.')**: - Thymeleaf 表达式引擎要求**运算符前后必须有空格** - 错误示例:`${object.property}`(点号周围无空格) - 触发引擎解析失败,因`.`被识别为非法运算符而非属性访问符 2. **表达式语法错误(got 'currentPage-1')**: - 在需要完整表达式的位置使用了**未包裹的运算式** - 错误示例:`th:href="@{/page/currentPage-1}"` - 引擎将`currentPage-1`视为字符串而非表达式 ### 解决方案 #### 1. 点号运算符修复(针对第一个错误) ```html <!-- 错误写法 --> <div th:text="${user.name}"></div> <!-- 正确写法:属性访问无需空格 --> <div th:text="${user.name}"></div> <!-- 点号在对象属性访问中是合法的 --> <!-- 但数学运算必须加空格 --> <span th:text="${2 * 3}"></span> <!-- 运算符*前后有空格 --> ``` #### 2. 表达式语法修复(针对第二个错误) ```html <!-- 错误写法 --> <a th:href="@{/page/currentPage-1}">上一页</a> <!-- 正确写法1:使用 $ 包裹表达式 --> <a th:href="@{'/page/' + ${currentPage - 1}}">上一页</a> <!-- 正确写法2:路径参数语法 --> <a th:href="@{/page/{id}(id=${currentPage - 1})}">上一页</a> ``` #### 3. 复杂表达式规范 ```html <!-- 三元运算符规范 --> <div th:class="${isActive} ? 'active' : 'inactive'"></div> <!-- 复合运算规范 --> <input th:value="${(item.price * item.quantity) + tax}"> ``` ### 关键语法规则 1. **运算符空格规则**: ```html <!-- 合法 --> <span th:if="${age >= 18}"> <!-- 非法(缺少空格) --> <span th:if="${age>=18}"> <!-- 触发 got '>' 错误 --> ``` 2. **表达式边界规则**: ```html <!-- 合法(完整表达式包裹) --> <div th:text="${@service.calculate(item)}"> <!-- 非法(片段表达式) --> <div th:text="@service.calculate(item)"> <!-- 触发 got '.' 错误 --> ``` ### 验证方法 1. 安装 **Thymeleaf 模板校验插件**(如 IDEA 的 Thymeleaf Linter) 2. 启用调试模式查看详细错误: ```properties # application.properties spring.thymeleaf.cache=false logging.level.org.thymeleaf=TRACE ``` ### 预防措施 1. 复杂表达式使用 **`()` 明确优先级**: ```html th:if="${(user.role eq 'admin') and (user.status eq 'active')}" ``` 2. 避免在表达式中直接使用未转义的保留字符(如`&&`, `||`) 3. 字符串拼接使用单引号: ```html th:href="@{'/api/' + ${endpoint} + '/data'}" ``` > **重要提示**:Thymeleaf 3.x 版本对语法要求更严格,升级后出现此类错误需重点检查运算符空格和表达式完整性[^1]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值