- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在链接我的程序时遇到问题。所有的类都可以很好地编译
g++ -c main.cpp
g++ -c Server.cpp
g++ -c Client.cpp
但是当我去链接它们的时候
g++ main.o Server.o Client.o -o main.out -lsfml-network -lsfml-system
我的客户端和服务器类中的函数得到了 undefined reference 。
main.cpp:(.text+0x1ba): undefined reference to `(anonymous namespace)::Server::Server()'
main.cpp:(.text+0x1c6): undefined reference to `(anonymous namespace)::Server::getMessage()'
main.cpp:(.text+0x210): undefined reference to `(anonymous namespace)::Server::~Server()'
main.cpp:(.text+0x227): undefined reference to `(anonymous namespace)::Client::Client()'
main.cpp:(.text+0x23d): undefined reference to `(anonymous namespace)::Client::sendMessage(std::string const&)'
main.cpp:(.text+0x287): undefined reference to `(anonymous namespace)::Client::~Client()'
main.cpp:(.text+0x3e9): undefined reference to `(anonymous namespace)::Server::~Server()'
main.cpp:(.text+0x407): undefined reference to `(anonymous namespace)::Client::~Client()'
collect2: error: ld returned 1 exit status
非常感谢任何帮助。
主要.cpp
#include <iostream>
#include <cstdio>
#include <string>
#include "include/Server.hpp"
#include "include/Client.hpp"
void printOptions(std::string const* const, size_t const&);
char const getInput(std::string const* options, size_t const& size);
int main()
{
const std::string YES_NO[] = {
"Yes",
"No"
};
const std::string OPTIONS[] = {
"Server",
"Client"
};
const std::string CONTINUE = "Continue?\n\n";
const std::string PROMPT = "Run as?\n";
const std::string RECIPEINT = "127.0.0.1";
const size_t SIZE = sizeof(OPTIONS) / sizeof(std::string);
std::cout << PROMPT;
const char INPUT = getInput(OPTIONS, SIZE);
char response;
// Server
if (INPUT == '1')
{
Server server;
do
{
server.getMessage();
std::cout << CONTINUE;
response = getInput(YES_NO, 2);
} while (response == '1');
}
// Client
else if (INPUT == '2')
{
Client client;
do
{
client.sendMessage(RECIPEINT);
std::cout << CONTINUE;
response = getInput(YES_NO, 2);
} while (response == '1');
}
// else serious problem
}
/* Function used to display a list of options to the user.
Each option is displayed on a new line preceeded with
its input number and provided option text.
Ex:
options[] = {"Option A", "Option B"}
Will print:
1) Option A
2) Option B
@param options
An array of std::string that will be displayed
as the list of options.
@param size
The number of different options the options array contains.
*/
void printOptions(std::string const* options, size_t const& size)
{
for (size_t i = 0; i < size; i++)
{
std::cout << i + 1 << ") ";
std::cout << options[i] << std::endl;
}
}
/* Used to return a users choice from a list of options.
*WARNING*
Providing an array with more than 9 options will give
unexpected return values.
If it is necesszary to provide a user with mroe than 9 options
have the 9th option be "More options..." from which you may call
getInput() again, with additonal options.
@param options
An array of std::string that will be displayed
for the users choice.
@param size
The number of different optins the optinos array contains.
@return const char
A number from 1 to 9 representing the users choice from
the options array.
1 = array index 0, 9 = array index 8.
*/
const char getInput(std::string const* options, size_t const& size)
{
printOptions(options, size);
char input;
bool needInput = true;
while (needInput)
{
std::cout << "Enter a number: ";
std::cin >> input;
for (size_t i = 0; i < size; i++)
{
char optionBuffer[2];
std::sprintf(optionBuffer, "%zu", i + 1);
if (input == *optionBuffer)
{
needInput = false;
break;
}
}
if (needInput)
std::cout << "Option not available." << std::endl;
}
return input;
}
服务器.hpp
#pragma once
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
namespace
{
class Server
{
public:
Server();
~Server();
static const unsigned short SERVER_PORT = 54000;
void getMessage();
protected:
private:
sf::UdpSocket socket;
};
};
服务器.cpp
#include "include/Server.hpp"
//using namespace;
Server::Server()
{
this->socket.bind(SERVER_PORT);
}
Server::~Server() {}
// TODO: Ensure that the socket is bound to a port.
void Server::getMessage()
{
sf::IpAddress sendersAddress;
unsigned short sendersPort;
sf::Packet sendersPacket;
// Failed to recieve packet
if (this->socket.receive(sendersPacket, sendersAddress, sendersPort)
!= sf::Socket::Done
)
{
std::cout << "Failed to recieve packet." << std::endl;
}
// Sucessfully recievd packet
else
{
std::string message;
sendersPacket >> message;
std::cout << "Recieved message:\n\n" << message << std::endl;
}
}
客户端.hpp
#pragma once
#include "Server.hpp"
#include <SFML/Network.hpp>
namespace
{
class Client
{
public:
Client();
~Client();
void sendMessage(std::string const&);
protected:
private:
sf::UdpSocket socket;
};
};
客户端.cpp
#include "include/Client.hpp"
//using namespace;
Client::Client()
{
this->socket.bind(sf::Socket::AnyPort);
}
Client::~Client()
{
}
void Client::sendMessage(std::string const& recipient)
{
std::string message;
sf::Packet packet;
std::cout << "Write a message:\n" << std::endl;
do
{
std::getline(std::cin, message);
} while (!message.size());
socket.send(packet, recipient, Server::SERVER_PORT);
}
最佳答案
问题是您的 Server
和 Client
类声明位于未命名(或匿名) namespace 内。要修复它,只需为您的 namespace 命名,例如:
namespace MyNamespace
{
class Server
{
// ...
};
}
在*.cpp
文件(包括main.cpp
)中,确保有相应的using
指令,例如:
using namespace MyNamespace;
或者,您可以在使用名称时完全限定名称,例如MyNamespace::Server
而不仅仅是 Server
。
作为旁注,未命名的命名空间实际上是有效的,有时非常有用。当编译器看到一个没有名字的命名空间时,它会为它想出一个唯一的内部名称,并立即在它后面跟一个隐藏的 using namespace ...
指令。这对于您只想在单个 *.cpp
文件中定义和使用的内容非常有用,因为它可以帮助避免命名冲突。 (做类似事情的旧方法涉及 static
关键字)。
但作为规则,不要在头文件中使用未命名的命名空间。
关于c++ - undefined reference 错误c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19849071/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!